3

Assume proper headers are included.

I have a class for various functions of a calculator. I have several structs for different operator types (nullary, Unirary, Binary, Ternary). I want to initialize that a vector(or preferably an array) of the structs with the elements filled in for w/e my calculator supports. For example nul[]={{pi, "pi"}, {ans, "ans"}}; My program will then take a token from the input and search for the appropriate operator, return the index of that operator, and then i'd invoke the proper function pointer. My problem is initializing an array or vector in the constuctor. I'd appreciate it if someone could show me both ways. thank you. Below is my class and unfinished constructor. Also if u can think of a better way for me to write my calculator I'd love to hear it.

//operator class
class Functions{
    public:
        Functions();
        double pi(double x=0);
        double answer(double& x);
        double abs(double& x);
        double pow(double& x);
        double add(double& x, double &y) const;
        double subtract(double& x, double& y) const;
        double multiply(double& x, double& y)const;
        double divide(double& x, double& y) const;
        double max(double& x, double& y, double& z) const;
        double volume(double& x, double& y, double& z) const;
        void doc();
        bool weird(double &x) const;
        unsigned findop0(const string & name);
        unsigned findop1(const string & name);
        unsigned findop2(const string & name);
        unsigned findop3(const string & name);
    private:
        struct Nul{
            double (*nulf)(double & x);
            string name;
        };
        struct Uni{
            bool (*unif)( double & result, double x );
            string name;
        };
        struct Bin{
            bool (*binf)( double & result, double x, double y );
            string name;
        };
        struct Tern{
            bool (*terf)( double & result, double x, double y, double z );
            string name;
        };
        static const unsigned NUL_ELEMENTS = 2;
        static const unsigned UNI_ELEMENTS = 2;
        static const unsigned BIN_ELEMENTS = 4;
        static const unsigned TRI_ELEMENTS = 2;
        vector<Nul> nul;
        vector<Uni> uni;
        vector<Bin> bin;
        vector<Tern> tri;
};

Functions::Functions(){
    nul
}
2
  • 3
    trivial with initializer lists in C++...otherwise just use push_back in a constructor function. Commented Oct 11, 2012 at 21:01
  • It's easy for vector with C++11. It was harder than it should have been before that. Commented Oct 11, 2012 at 21:16

1 Answer 1

3

In C++11 you can do this in one line, in initialization list.

struct C {
  int a[20];
  int* b;
  std::vector<int> c;
  std::array<int,20> d;
  C() : 
     a({1,2,3}), 
     b(new int[20]{1,2,3}), 
     c({1,2,3}), 
     d(std::array<int, 20u>{1,2,3}) 
  {}
};

In C++03 use multiline assignments or make static function to initialize vector or raw array.

struct C {
  int a[20];
  int* b;
  std::vector<int> c;
  C() : 
     b(new int[20]), 
  {
     a[0] = 1; ...
     b[0] = 1;
     c.push_back(1);
  }
};
Sign up to request clarification or add additional context in comments.

7 Comments

I hate to be a bother, but It still doesn't seem to be working. Functions::Functions() : nul({{pi,"pi"},{&answer,"ans"}}),uni({{&abs,"abs"},{&pow,"pow"}}), bin({{&add,"+"},{&subtract,"-"},{&multiply,"*"},{&divide,"/"}}), tri({{&max, "max"},{&vol,"vol"}}) {} the error i keep getting tells me Error 4 error C2143: syntax error : missing ';' before '}'
Which compiler? Does it support braces initialization for std::vector?
Another problem - your functions must be static to match your structs functions types.
I'm using visual studio 2012 that was supplied by my school. I'm guessing its just an annoying quirk with microsoft, right?
No, 2012 for sure support this, maybe you need to swtich on C++11?. You have a lot of other errors. See ideone.com/42oqY - it works. Some of your problem - method must be static and not const, you have mismatch between pi and answer. I fixed for nul vector - rest try yourself...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.