繁体   English   中英

CPP-编译错误(G ++)

[英]CPP - Compiling error (G++)

我使用Mac OSX。然后写了一个简单的程序。 但是,在终端中编译时出错。

我的终端代码是: 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中运行,并且不使用终端进行编译。

您的程序是从多个源文件构建的,所以最简单的方法是:

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等实用程序更好地完成

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM