-1

I have instructions to use a pointer of a pointer as an array of pointers and this is what I have:

className **wordArray;
wordArray = new className*[wordCount];
ifstream fileInput;
fileInput.open(fileDir);
while (fileInput >> wordInput)
{
    wordArray[countNumber] = new className(wordInput.c_str());
    countNumber++;
}

class className
{
public:
    className(const char *word);
    ~className();
};

className::className(const char *word)
{
    char wordArray[strlen(word)];
    strcpy(wordArray, *word);
}

My problem occurs when it compiles, or tries to. It says that there is an "undefined reference" to the class. The constructor for the class is supposed to take a const char* and I have tried a few other things to no avail.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Spartin503
  • 47
  • 3
  • 9
  • 2
    This is not C. removing the tag. – Sourav Ghosh Jan 13 '16 at 06:59
  • 3
    [What is undefined reference and how to fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – R Sahu Jan 13 '16 at 07:02
  • 2
    Please post a [MCVE](http://stackoverflow.com/help/mcve) so that others can more easily reproduce the problem if/as needed. – code_dredd Jan 13 '16 at 07:02
  • Apart from the obvious problem of writing what is supposed to be C++ code as if it were C, you have a serious bug here: `char wordArray[strlen(word)];` - this should be: `char wordArray[strlen(word) + 1];`. – Paul R Jan 13 '16 at 07:17

2 Answers2

1

undefined reference means that some function or variable was declared but not implemented. In this case you need to provide implementation of constructor, e.g.:

className::className(const char *word)
{
    // do something here...
}

And the same for destructor.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
1

You are getting an Undefined reference error because you are missing the implementation of className's constructor and destructor. Both can be empty, but must exist either inline, or as separate functions.

Inline:

class className {
public:
  className(const char *word) {}
  ~className() {}
};

Separate functions:

class className {
public:
  className(const char *word);
  ~className();
};

className::className(const char *word) {}
className::~className() {}

For me, this minimal working example compiles:

#include <string>

class className {
public:
  className(const char *word) {}
  ~className() {}
};

int main(int argc, char** argv) {
  className **wordArray;
  std::string wordInput = "test";
  int wordCount = 0;

  wordArray = new className*[wordCount];
  wordArray[0] = new className(wordInput.c_str());
}
  • I realized I had made about the stupidest mistake I have ever made. I forgot to include the other .cpp file in the command to compile it. Although, now that I did, I have an error that says "expected unqualified-id before 'const'" and "expected ')' before 'const'". – Spartin503 Jan 13 '16 at 07:21
  • The problem seems to not be related to this question. Please search for questions alike, and if you don't find any matching your problem, ask a new question together with an [MCVE](http://stackoverflow.com/help/mcve). –  Jan 13 '16 at 08:32