CPP - 编译错误(G ++)

时间:2014-10-14 19:20:14

标签: c++ xcode macos gcc

我使用Mac OS X.然后我写了一个简单的程序。但是,我在终端编译时遇到错误。

我的终端代码为:g++ main.cpp -o main

然后错误是:

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)

我不明白这个错误,因为当我在XCODE6中构建它时,它不会给出任何错误。

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

我不明白这个问题。我可以在Xcode中运行,我不用终端编译.. 亲切的问候。

1 个答案:

答案 0 :(得分:4)

您的程序是由多个源文件构建的,因此最简单的方法是:

g++ main.cpp TestBed.cpp -o main

如果你使用它们,就会以相同的方式放置更多的.cpp文件。

将每个.cpp文件编译成.o文件然后将它们链接在一起的更好方法:

g++ -c main.cpp 
g++ -c TestBed.cpp
g++ main.o Testbed.o -o main

然后,如果您更改一个源,则无需重新编译所有内容。但是,make或您的IDE

等实用程序可以更好地完成这项工作