Linker error with pointers
-
18-09-2019 - |
Question
I am trying to build a program to Tolkinize a string (i'm trying to do this with out using the string class - so as to learn more about pointers and how chars work) - I have built a program that i think works (any suggestions would be great!) When i tried to compile the program I get these random errors:
Error 1 error LNK2019: unresolved external symbol "public: char * __thiscall Tokenize::next(void)" (?next@Tokenize@@QAEPADXZ) referenced in function _main Driver
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Tokenize::Tokenize(char const * const,char)" (??0Tokenize@@QAE@QBDD@Z) referenced in function _main Driver
Error 3 fatal error LNK1120: 2 unresolved externals C:\Users\Simmons 2.0\Documents\School\CS1410\project 5\Token\Debug\Token.exe
I have showed this code to a couple of friends that code and none of them could figure out what is going on - or what i'm doing wrong...
Note: I am running VS 2008 express edition - Windows 7 x64Main.cpp
#include <iostream>
#include "tokenize.h" /// Tolkenize class
using namespace std;
int main ( )
{
// create a place to hold the user's input
// and a char pointer to use with the next( ) function
char words[128];
char * nextWord;
cout << "\nString Tokenizer Project";
cout << "Enter in a short string of words:";
cin.getline ( words, 127 );
// create a tokenizer object, pass in the char array
// and a space character for the delimiter
Tokenize tk( words, ' ' );
// this loop will display the tokens
while ( ( nextWord = tk.next() ) != NULL ) {
cout << nextWord << endl;
}
system("PAUSE");
return 0;
}
tokenize.h
#include <iostream>
#include <cassert> /// assert
#include <cstdlib> /// system("cls")
using namespace std;
#ifndef TOKENIZE_H
#define TOKENIZE_H
#include <iostream>
#include <cassert>
#include "Tokenize.h"
using namespace std;
class Tokenize {
private:
char * current_ptr;
char delimiter;
public:
Tokenize ();
Tokenize (char);
Tokenize (char const string [], char Delimiter);
void setcurrent_ptr ( char * ptr ){ current_ptr = ptr; }
void setdelimiter ( char Delimiter ) { delimiter = Delimiter; }
char * getcurrent_ptr () { return current_ptr; }
char getdelimiter () { return delimiter; }
char * next ();
};
#endif
tokenize.cpp
#include <iostream>
#include <cassert> /// assert
#include "Tokenize.h"
using namespace std;
Tokenize::Tokenize() {
current_ptr = new char;
*current_ptr = NULL;
delimiter = ' ';
};
Tokenize::Tokenize(char Delimiter) {
current_ptr = new char;
*current_ptr = NULL;
delimiter = Delimiter;
};
Tokenize::Tokenize(char const string [], char Delimiter) {
current_ptr = string;
delimiter = Delimiter;
};
char * Tokenize::next() {
char * ptr = current_ptr;
If ( (*ptr) == NULL ) { return NULL; }
else {
while ((current_ptr)++ != ' ') {}
if ( (*current_ptr) == NULL) { return NULL; }
if ( *current_ptr == ' ' ) { *current_ptr = '/0'; (current_ptr)++; }
return ptr;
}
};
From my testing (commenting out lines) I think one of the errors come from the tokinze.next()
Solution
It looks like you're not compiling/linking tokenize.cpp.
It's probably not the code itself, but how you have it setup in Visual Studio.
Is it possible that VS isn't including tokenize.cpp in your application?
You can test that this is the problem by moving the functions from tokenize.cpp to main.cpp. If the errors go away, that's the problem.
OTHER TIPS
Not to do with the linker but in your header Tokenizer.h you say:
#include "Tokenize.h"
what is Tokenize.h? You haven't shown it. also, your include guards seem to be guarding against this file.
There were several problems with your posted code. In order of discovery:
Tokenize::Tokenize(char const string [], char Delimiter) {
current_ptr = string;
delimiter = Delimiter;
};
You are trying to assign a const pointer to a non-const member variable. Can't do this; change the definition of the Tokenize ctor to:
Tokenize::Tokenize(char * const string , char Delimiter) {
current_ptr = string;
delimiter = Delimiter;
};
In Tokenize::next() change:
while ((current_ptr)++ != ' ') {}
...to:
while (*(current_ptr++) != ' ') {}
And just below that you have a bad escape sequence. Change:
if ( *current_ptr == ' ' ) { *current_ptr = '/0'; (current_ptr)++; }
...to:
if ( *current_ptr == ' ' ) { *current_ptr = '\0'; (current_ptr)++; }
Once I fixed these errors, your code compiled fine. I didn't get the unresolved external error you were getting, so you must not be compiling tokenize.cpp