سؤال

I am still fairly new to NetBeans, and am writing code for class in C++. I am currently on my third project, and I have run into an error I can't seem to resolve when trying to compile+run my project. I have quadruple-checked my code, going so far as to copy code from a previous project. I have tried quiting, rebooting the computer, and starting NetBeans up again. I ran CppCheck on my code and it found no errors.

The error message:

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()'

I tried copying code from a previous project, and even with the exact same code as a previous project which works, it's still having this problem. Basically, the build is failing to recognize the Dictionary class.

What things can I check that might cause this problem? Any obscure (or even obvious) settings I can check? Should I just start a new project and copy my code over?

Edit: Adding main():

#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;
}

And Dictionary.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 */

And Dictionary.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;
    }
}
هل كانت مفيدة؟

المحلول

Found my problem. Netbeans didn't consider the Dictionary class to be part of my project, so it wasn't compiling Dictionary.cpp. I added it in the Project window by right-clicking the Source Files folder and using Add existing item... menu option. Now it compiles fine.

Does anyone know why the class wouldn't be added if I used Netbean's New File interface and added to the project specifically?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top