[英]Vector definition in .h and .cpp file
So I was going through a text on C++ and noticed the following code: 因此,我正在阅读有关C ++的文本,并注意到以下代码:
// 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()
. 如您所见,在.h文件中对vector的类似定义给了我错误vecSize is not a typename
而如果我尝试在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. 您已经缺少了Example::vec
的定义,并且对于复杂类进行声明内联初始化是不可能的。
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). 这是因为,在h文件中,您正在类中声明事物,并且除非定义了变量,否则不允许对其进行初始化,因为它不是const(即使大多数静态const事物也不能在那里进行初始化)。 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. 由于h文件可以包含在最终链接在一起的不同c文件中,因此,如果在那里有变量的定义(“实际内存”),则会得到重复。 You can have multiple declarations, but only one definition. 您可以有多个声明,但只能有一个定义。 Normally, I'd do this for that situation: 通常,对于这种情况,我会这样做:
.h file .h文件
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 .c文件
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. 如果类A包含类B,则类A的所有实例都包含类B的实例。实例化时将分配内存。 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. 但是它们需要在类定义之外的.c文件中进行设置,因为否则,当另外2个库的代码都包含.h文件时,它们各自都有自己的内存副本,这会使链接程序感到困惑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.