undefined reference to constructor

Hi I'm afraid that the following code is giving me a "undefined reference to 'strSet::strSet(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'"

The following is a header file for an object strSet which stores a vector of strings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//strSet.h
#ifndef _STRSET_
#define _STRSET_

#include <iostream>
#include <vector>
#include <string>

class strSet
{
private:
    std::vector<std::string> strVector; 
    bool isSorted () const;

public:
    strSet ();  // Create empty set
    strSet (std::string s); // Create singleton set

 ... excess code


This is the implementation of my constructors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 // strSet.cpp
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include "strset.h"
using namespace std;

bool strSet::isSorted() const {
	return true;
}

strSet::strSet() { }

strSet::strSet(std::string s) { 
	strVector.push_back(s);
}


And finally in my main function I call the constructor as :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// setcalc.cpp
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <vector>
#include "strset.h"
#include "help.h"

using namespace std;

struct eleOfSet {
	string setName;
	strSet setValue;
 };

int main (int argc, char* argv[]) {

	bool vflag = false;
	char value;
	string tempName;
	strSet *tempSet;
	eleOfSet tempEle;
	vector<eleOfSet> setOfSets;

	tempSet = new strSet("hello");
	
	delete tempSet;
	return 0;
}


Although I've clearly defined both constructors in the header file and respective cpp file, it tells me that the constructor is undefined. Is there any particular reason why?
Last edited on
Have you tried tempSet = new strSet(string("hello"));? Also, change the constructor to taking a const reference.
Thanks for the reply, unfortunately I have no control over changing the constructor parameters since I am not allowed to change the header file.

However changing it to tempSet = new strSet(string("hello")); does not fix the problem.

To be more precise this is not the only error I am getting since I also get a similar "undefined reference" error in my struct definitions:

/tmp/ccuWFpZX.o: In function 'eleOfSet::eleOfSet()':
setcalc.cpp:(.text._ZN8eleOfSetC1Ev[eleOfSet::eleOfSet()] + 0x27): undefined reference to 'strSet::strSet()'
Well then, do you actually link strSet.cpp to your main program?
LOL thank you very much.
I changed #include "strset.h" with #include "strset.cpp" and it worked fine. My apologies I just don't entirely understand the linking step from the compiler.

When I said #include "strset.h" doesn't it just refer to the header functions, and subsequently find a .cpp file where they are defined?

The reason why I wrote this is because I know it is customary to write #include <string.h> to use strcmp function. So when is it useful to include headerfiles as aposed to .cpp files?
No, don't do that. Linking is done by passing parameters to the compiler, not by inclusion. You do NOT include cpp files. (well, you can, but that defeats their entire point).

Please tell me what compiler you are using.
I'm using a g++ compiler.

The code for strset.cpp and strset (at least for the constructors) should be written in the first post.
Oh I feel as though I might have left out something crucial: I infact over wrote the assignment operator function in strSet:

1
2
3
4
strSet& strSet::operator = (const strSet& rtSide) {
	strVector = rtSide.strVector;
	return *this;
}	


I don't know how badly this might affect my problem however.
Last edited on
I had this exact same problem. The solution was that I forgot to set a target for my CPP file when linking it to the project. What compiler are you using?
I'm using a g++ compiler. What is the proper way to link this?
1
2
g++ -c *.cpp #compiling
g++ *.o -o program.bin #linking 

An IDE should do that for you. (create new project, add file to project, ...)
Or make a makefile.
I think he doesn't really know what exactly linking is yet. See here for a general explanation: http://en.wikipedia.org/wiki/Linker_%28computing%29

in g++, you would normally do it like that:
1
2
3
4
5
g++ -c file1.cpp #creates file1.o
g++ -c file2.cpp #creates file2.o
g++ file1.o file2.o -o program # links file1.o and file2.o together to form the executable program
#You can also write it like this
g++ file1.cpp file2.cpp -o program


Basically, this is done to be able to spread your code on multiple files so you don't have to recompile the entire program again after changing just one line... For example, if you have file1.cpp and file2.cpp, you would compile them to get file1.o and file2.o. Linking them gives you the executable. Now if you change file1.cpp you only need to recompile file1.cpp, and link the new file1.o to the old file2.o to create the new executable. Header files are there to provide the interface to an object file, basically so the compiler knows what symbols he can use.
Thank you very much for the explanation. It works now hanst99, using the last compile command you wrote.
An IDE should do that for you. (create new project, add file to project, ...)


+1

Coding without an IDE is masochism.
Topic archived. No new replies allowed.