0

我使用 g++ 编译器并使用终端来编译单个 c++ 文件和项目(在项目下,我指的是同一目录中的文件,但不是真正的 Xcode 项目)。我没有问题,但我升级到 OS X Mavericks,从那时起,我只能编译单个文件。之后,我安装了 Xcode 5.0.1,并安装了命令行工具,但没有解决我的问题。

所以现在我使用的是 OS X 10.9 Mavericks Xcode 5.0.1 (5A2053)。

我想,问题出在我的源代码上,但现在我做了一个非常简单的程序,但我得到了同样的错误:

Steve-MacBook:test szaboistvan$ g++ -o main main.cpp
Undefined symbols for architecture x86_64:
  "Myclass::geta()", referenced from:
      _main in main-0bDtiC.o
  "Myclass::getb()", referenced from:
      _main in main-0bDtiC.o
  "Myclass::Myclass()", referenced from:
      _main in main-0bDtiC.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

项目文件:main.cpp

#include <iostream>
#include "Myclass.h"
using namespace std;



int main(){

    Myclass one;
    cout<<one.geta()<<endl<<one.getb()<<endl;

    return 0;
}

我的班级.h

#include <iostream>

class Myclass
{
private:
    int a;
    double b;
public:
    Myclass();
    int geta();
    double getb();
};

我的类.cpp

#include <iostream>
#include "Myclass.h"
using namespace std;

Myclass(){
    int a=5;
    double b=3.0;
}

int geta(){
    return a;
}

double getb(){
    return b;
}

先感谢您!

4

1 回答 1

1

Unless I am being thick (hint, I am not), you havent implemented

MyClass::geta(). Instead, you implemented a function called geta

Your class methods should be like:

MyClass::Myclass()
{
    int a=5;
    double b=3.0;
}

int MyClass::geta()
{
    ....
}

etc

In addition, you cant be compiling MyClass.cpp, since the code in there is not valid.

于 2013-10-29T12:22:19.563 回答