简体   繁体   中英

Vector definition in .h and .cpp file

So I was going through a text on C++ and noticed the following code:

    // example.h
    class Example {
    public:
    static const double rate = 6.5;
    static const int vecSize = 20;
    static vector<double> vec(vecSize); // Error! 
    };

//myfile.cpp
  int main()
{
    int vecsize=20;
    vector<double> v(vecsize); // OK!

    return 0;
}

As you can see the comments, a similar kind of definition for vector in .h file gives me the error vecSize is not a typename whereas it is perfectly valid if I try to do something like this in main() .

Why is so happening? Any help? I guess I am missing something, there is something that I don't know. Thanks.

You change your code as follows:

    // example.h
    class Example {
    public:
    static const double rate = 6.5;
    static const int vecSize = 20;
    static vector<double> vec; // <<<<<
    };

    vector<double> Exanple::vec(vecSize);

You've been missing the definition for Example::vec , and it's not possible that way with a declaration inline initialization for complex classes.

That's because, in the h file, you're making a declaration of things in your class, and your variable isn't allowed to be initialized unless it's defined, since it isn't const (and even most static const things can't be initialized there, either). Since h files can be included by different c files that eventually get linked together, you get duplicates if you have your definitions (the "actual memory") for your variables there. You can have multiple declarations, but only one definition. Normally, I'd do this for that situation:

.h file

class Example {
public:
    static const double rate = 6.5; // static const number: should be OK
    static const int vecSize = 20;  // ditto. Could move these to C file, too, though.
    static vector<double> vec;      // declared, but not defined or initialized.
}

.c file

vector<double> Example::vec(vecSize); // compiles into a library, only exists in 1 place.

In main, however, you're defining an object that's an instantiation of a vector (a thing with memory allocated to it), so you can initialize it, there.

[After having submitted this, I saw your comments above]

A class can contain another class because classes are like blueprints for objects. If class A contains class B, all instances of class A contain an instance of class B. The memory gets allocated when they're instantiated. There is memory for the class itself: function definitions and static variables. But they need to be set up outside the class definition, in the .c file, because otherwise, when 2 other libraries have code that includes your .h file, they each have their own copy of the memory, which makes the linker confused.

You probably forgot to #include <vector> and you should write std::vector<double> instead of vector<double> .

You should want to switch to C++11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM