简体   繁体   English

.h和.cpp文件中的向量定义

[英]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文件时,它们各自都有自己的内存副本,这会使链接程序感到困惑。

You probably forgot to #include <vector> and you should write std::vector<double> instead of vector<double> . 您可能忘记了#include <vector> ,应该编写std::vector<double>而不是vector<double>

You should want to switch to C++11 您应该要切换到C ++ 11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在.h文件中声明矢量,在.cpp文件中初始化 - Declaring a vector in a .h file, initialize in a .cpp file * .h文件和* .cpp文件中的派生类的定义和声明 - Definition and Declaration of derived classes in *.h file and *.cpp file Visual Studio 2017 - 转到定义转到 .h 文件而不是 .cpp - Visual Studio 2017 - Go To Definition goes to .h file instead of .cpp #include .h或.cpp文件? - #include .h or .cpp file? .h中的声明 .cpp中的定义。 正确? - Declaration in .h. Definition in .cpp. Correct? .cpp与.h中的函数方法定义 - Function method definition in .cpp vs .h 在.h文件的类定义中声明但未在.cpp中定义的函数的C ++函数原型 - C++ function prototype declared in class definition in .h file but function not defined in .cpp 如何通过包含相应的.h文件从.cpp中获得函数/类定义? - How do I get a function/class definition from a .cpp by including its corresponding .h file? 使用 IMPLICIT 实例化而不是 .H 允许在 CPP 文件中放置类成员函数的 C++ 模板定义? - Placing C++ template definition for a class member function in CPP file with IMPLICIT instantiation instead of .H allowed? 为什么将operator。()的定义从.h移到.cpp文件会导致数据争用? - why is moving the definition of the operator() from .h to .cpp file causing a data race?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM