1

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;
    }
}
Mar
  • 7,765
  • 9
  • 48
  • 82
  • 1
    Those are linker errors. Nothing to do with Netbeans. Could you show the definitions for your class and how you including them in main.cpp? – Jesse Good May 08 '12 at 05:10
  • Added main() and Dictionary.h – Mar May 08 '12 at 05:25
  • Do you have a `Dictionary.cpp`? Where are the definitions for Dictionary? – Jesse Good May 08 '12 at 05:28
  • I don't *think* it's the cause of the issue, but don't use `using namespace` in header files - see http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c – John Carter May 08 '12 at 05:45
  • Strange, the only thing I can recommend is right clicking `Dictionary.cpp` from the project list and make sure it isn't being excluded from the build (everything you shown works fine for me). – Jesse Good May 08 '12 at 06:28
  • I have another project with a different Dictionary class (same name, different members). Is it possible Netbeans is getting them mixed up? Also, I noticed in the "Navigator" window it says "The file with restricted code assistance" in red at the top of the list of class members for Dictionary.cpp. – Mar May 09 '12 at 01:49

1 Answers1

1

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?

Mar
  • 7,765
  • 9
  • 48
  • 82
  • I had the same problem... I had to remove the files first from the project and add them again. It looks like a bug in NetBeans. I added 5 files to my project: 3 were added correctly, 2 were not... – AbcAeffchen Sep 22 '14 at 02:57