Reputation: 139
I use Mac OS X. Then I wrote a simple program. However, I have an error with compiling in terminal.
My terminal code is : g++ main.cpp -o main
Then error is:
Undefined symbols for architecture x86_64:
"TestBed::TestBed()", referenced from:
_main in main-3003ff.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I don't understand this error because when I build it in XCODE6 it doesn't give any error.
TestBed.cpp:
#include "TestBed.h"
using namespace std;
TestBed::TestBed(){
}
void TestBed::execute(){
int x ;
x = algorithm->select();
cout << "x: " << x;
}
void TestBed::setAlgorithm(int type, int k){
if(type==1){
algorithm = new AlgorithmSortAll(k);
}else if(type==2){
algorithm = new AlgorithmSortK(k);
}
}
TestBed::~TestBed(){
}
TestBed.h:
#ifndef TestBed__
#define TestBed__
#include <iostream>
#include "SelectionAlgorithm.h"
#include "AlgorithmSortAll.h"
#include "AlgorithmSortK.h"
class TestBed{
private:
SelectionAlgorithm *algorithm;
public:
//If I delete virtual keyword in execute,setAlgorithm,~TestBed
//It gives 3 more errors.
virtual void execute();
virtual void setAlgorithm(int type, int k);
TestBed();
virtual ~TestBed();
};
#endif
main.cpp:
#include <iostream>
#include "TestBed.h"
using namespace std;
int main() {
TestBed *tb = new TestBed();
int algorithm_type;
cin >> algorithm_type;
int k;
cin >> k;
tb->setAlgorithm(algorithm_type, k);
tb->execute();
delete tb;
return 0;
}
AlgorithmSortAll.cpp: #include "AlgorithmSortAll.h"
AlgorithmSortAll::AlgorithmSortAll(int k) : SelectionAlgorithm(k){
int N;
std::cin >> N;
int *pNums = 0;
pNums = new int[N];// Allocate n ints and save the pointer in pNums
for (int i=0; i<N; i++) {
int number;
std::cin >> number;
pNums[i] = number;
}
//Sorting
int i, j, moved;
for (i = 0; i < N; i++) {
moved = pNums[i];
j = i;
while (j > 0 && pNums[j - 1] > moved) {
pNums[j] = pNums[j - 1];
j--;
}
pNums[j] = moved;
}
//Assignin k
SelectionAlgorithm::k = pNums[k];
delete [] pNums; // When done, free the memory pointed to by pNums
pNums = 0;
}
int AlgorithmSortAll::select(){
return SelectionAlgorithm::k;
}
AlgorithmSortAll::~AlgorithmSortAll(){
}
AlgorithmSortAll.h:
#ifndef AlgorithmSortAll__
#define AlgorithmSortAll__
#include "SelectionAlgorithm.h"
class AlgorithmSortAll : public SelectionAlgorithm{
public:
virtual int select();
AlgorithmSortAll(int k);
virtual ~AlgorithmSortAll();
};
#endif
AlgorithmSortK.cpp:
#include "AlgorithmSortK.h"
AlgorithmSortK::AlgorithmSortK(int k) : SelectionAlgorithm(k){
}
int AlgorithmSortK::select(){
return SelectionAlgorithm::k;
}
AlgorithmSortK.h:
#ifndef AlgorithmSortK__
#define AlgorithmSortK__
#include "SelectionAlgorithm.h"
class AlgorithmSortK : public SelectionAlgorithm{
public:
int select();
public:
AlgorithmSortK(int k);
};
#endif
I don't understand the problem. I can run in Xcode and I don't compile with terminal.. Kind Regards.
Upvotes: 0
Views: 244
Reputation: 44258
Your program is built from multiple source files, so simplest way would be:
g++ main.cpp TestBed.cpp -o main
and put more .cpp files there the same way if you use them.
better way to compile each .cpp file into .o file and then link them together:
g++ -c main.cpp
g++ -c TestBed.cpp
g++ main.o Testbed.o -o main
Then if you change one source you do not have to recompile everything. But that can be better done by utilities like make
or your IDE
Upvotes: 4