在 c++ 中使用静态属性?

发布于 2025-01-02 13:06:14 字数 1633 浏览 3 评论 0原文

我有一个作业,我需要从不同的文档中读取单词并将它们存储在一个字符串向量中,我将这个向量设为静态,以便每个文档只需将它们的单词添加到向量中,这样我就可以获得所有单词的一个列表字。 我创建了一个文档类,并在标题中写道:

class document {
public:

    document(string filename);

    static vector<string> words;
    string name;
    vector<int> frequency;
    void getFrequency();
    static void  addWord(string wordd); 

在 document.cpp 文件中使用以下内容实现了 addWord 方法:

 static void  document::addWord(string wordd){


    vector<string>::iterator i = find(words.begin(), words.end(), wordd);

    if (i == words.end()) {
        words.push_back(wordd);
    }
 }

但是这不起作用,每次我尝试构建代码时它都会给我这个错误消息

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf "/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/assignment1 mkdir -p build/Debug/GNU-MacOSX rm
-f build/Debug/GNU-MacOSX/main.o.d g++    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp mkdir -p dist/Debug/GNU-MacOSX g++     -o dist/Debug/GNU-MacOSX/assignment1 build/Debug/GNU-MacOSX/main.o   Undefined symbols for architecture x86_64:   "document::words", referenced from:
      document::getFrequency()      in main.o
      document::addWord(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in main.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status make[2]: *** [dist/Debug/GNU-MacOSX/assignment1] Error 1 make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 1s)

i have an assignment where i need to read the words from different documents and store them in a vector of strings, i made this vector static so that each document would just add their words to the vector so that i can have one list of all the words.
i made a document class and in the header i wrote :

class document {
public:

    document(string filename);

    static vector<string> words;
    string name;
    vector<int> frequency;
    void getFrequency();
    static void  addWord(string wordd); 

in the document.cpp file implemented the addWord method with the following :

 static void  document::addWord(string wordd){


    vector<string>::iterator i = find(words.begin(), words.end(), wordd);

    if (i == words.end()) {
        words.push_back(wordd);
    }
 }

however this doesn't work and every time i try to build the code it gives me this error message

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf "/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/assignment1 mkdir -p build/Debug/GNU-MacOSX rm
-f build/Debug/GNU-MacOSX/main.o.d g++    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp mkdir -p dist/Debug/GNU-MacOSX g++     -o dist/Debug/GNU-MacOSX/assignment1 build/Debug/GNU-MacOSX/main.o   Undefined symbols for architecture x86_64:   "document::words", referenced from:
      document::getFrequency()      in main.o
      document::addWord(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in main.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status make[2]: *** [dist/Debug/GNU-MacOSX/assignment1] Error 1 make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 1s)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉 2025-01-09 13:06:14

此错误消息所涉及的直接错误是您已声明 document::words 但尚未定义它。它的定义位于类定义之外,通常位于类的翻译单元中。您需要有如下所示的定义:

static std::vector<std::string> document::words;

也就是说,请不要认为静态数据非常类似于受控更好的全局数据。特别是对于并发程序来说,拥有所有对象共享的东西是一个坏主意。如果这个东西是可变的,那就是一个更糟糕的想法(顺便说一句,如果全局数据被称为“单例”,这也适用:仅仅因为某些东西据称是一种设计模式并不意味着它在某种程度上受到了祝福并且问题消失了离开)。

The immediate error which this error message is about is that you have declared document::words but you haven't defined it. A definition of this lives outside the class definition, typically in the class's translation unit. You need to have definition which looks something like this:

static std::vector<std::string> document::words;

That said, please not that static data is pretty much like slightly better controlled global data. Especially with concurrent programs having anything which is shared for all object is a Bad Idea. If this thing is mutable it is an even worse idea (BTW, the also applies if the global data is referred to as "Singleton": just because something is allegedly a design pattern doesn't mean it is somehow blessed and the problems are gone away).

爱情眠于流年 2025-01-09 13:06:14

从函数定义(来自 .cpp 文件)中删除关键字 static

当在函数定义中使用static时,该词的含义是该函数仅在当前编译单元中可见(类似于未命名命名空间)。因此,只需将其从 .cpp 中删除即可。

Remove keyword static from function definition (from .cpp file).

When static is used in function definition then the meaning of the word is that function is visible only in current compilation unit (similar to unnamed namespace). So, just remove it from .cpp.

错々过的事 2025-01-09 13:06:14

该行

Undefined symbols for architecture x86_64:   "document::words"

告诉您,您的静态数据成员从未实际创建:您在标头中声明了它,但从未告诉编译器在哪里存储该对象。

在您的情况下,这不是问题,但在具有许多包含相同标头的 .cpp 文件的大型项目中,重要的是静态对象仅分配一次。

将此行添加到 document.cpp 中:

vector<string> document::words;

除了作用域名称之外,它与全局变量声明完全相同(因为这实际上就是静态成员)。

The line

Undefined symbols for architecture x86_64:   "document::words"

is telling you that your static data member was never actually created: you declared it in the header, but never told the compiler where to store the object.

This is sort of a non-problem in your case, but in a larger project with many .cpp files including the same header, it's important the static object is only allocated once.

Add this line to document.cpp:

vector<string> document::words;

It's exactly like a global variable declaration (since that's really what a static member is), except for the scoped name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文