NetBeans 在生成+运行时无法识别C++类成员

NetBeans not recognizing C++ class members at build+run

本文关键字:识别 C++ 成员 运行时 NetBeans      更新时间:2023-10-16

我对 NetBeans 还比较陌生,正在用 C++ 编写类代码。我目前正在进行第三个项目,在尝试编译+运行我的项目时遇到了一个似乎无法解决的错误。我已经四次检查了我的代码,甚至从以前的项目中复制代码。我尝试退出、重新启动计算机并重新启动 NetBeans。我在代码上运行了 CppCheck,它没有发现任何错误。

错误消息:

build/Debug/MinGW-Windows/main.o: In function `main':
C:/Users/Martin/Documents/NetBeansProjects/Lab3/main.cpp:52: undefined reference to `Dictionary::Dictionary()'
C:/Users/Martin/Documents/NetBeansProjects/Lab3/main.cpp:52: undefined reference to `Dictionary::~Dictionary()'

我尝试从以前的项目中复制代码,即使使用与以前的项目完全相同的代码,它仍然存在此问题。基本上,构建无法识别字典类。

我可以检查哪些可能导致此问题的内容?我可以检查任何晦涩(甚至明显)的设置?我应该启动一个新项目并复制我的代码吗?

编辑:添加主():

#include <cstdlib>
#include <iostream>

#include "Dictionary.h"
using namespace std;
/*
 * argv[1] dictionary file
 * argv[2] boggle board file
 * argv[3] output file
 */
int main(int argc, char** argv) {
    if (argc > 3) {
        Dictionary dict;
        dict.loadDictFile(argv[1]);
    } else {
        cout << "Not enough arguments. Needed: ./lab3 [dictionary file] "
                "[board file] [output file]" << endl;
    }
    return 0;
}

和字典.h:

#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
#include <set>
using namespace std;
class Dictionary {
public:
    Dictionary();
    Dictionary(const Dictionary& orig);
    virtual ~Dictionary();
    virtual void loadDictFile(char * fileName);
    virtual bool find(string word);

private:
    set<string> dict;
    set<string> fullDictionary; // Contains all words, not just those 4+ char long.
};
#endif  /* DICTIONARY_H */

和字典.cpp:

#include "Dictionary.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <set>
//using namespace std;
Dictionary::Dictionary() {
}
Dictionary::Dictionary(const Dictionary& orig) {
    dict = orig.dict;
    fullDictionary = orig.fullDictionary;
}
Dictionary::~Dictionary() {
}
void Dictionary::loadDictFile(char* fileName) {
    ifstream infile;
    infile.open(fileName);
    if (infile) {
        while(!infile.eof()) {
            string line;
            getline(infile, line);
            fullDictionary.insert(line);
            if (line.size() > 3) {
                dict.insert(line);
            }
        }
    } else {
        cout << "Dictionary File not loaded: " << fileName << endl;
    }
}
bool Dictionary::find(string word){
    if (dict.find(word) != dict.end()) {
        return true;
    } else {
        return false;
    }
}

找到了我的问题。Netbeans 不认为 Dictionary 类是我项目的一部分,因此它没有编译 Dictionary.cpp。我通过右键单击Source Files文件夹并使用菜单选项将其添加到Project窗口中Add existing item...。现在它编译得很好。

有谁知道如果我使用 Netbean 的 New File 界面并专门添加到项目中,为什么不会添加该类?