C ++未定义引用...(Atom)

时间:2017-02-03 13:53:46

标签: c++ c++11 atom-editor

我使用Atom作为我的c ++ IDE,一个普通的hello world项目工作正常,如果我在头文件中定义了所有内容并且在cpp文件中没有做任何事情我可以使用一个类..我试过了从其他人那里获取工作代码,但它仍然无法正常工作,所以我很确定它的编译错误或者某些东西,请帮忙!下面是错误:

/tmp/ccXIaLmD.o: In function `main':
Main.cpp:(.text+0x82): undefined reference to `Name::Name(std::__cxx11::basic_string<char, std::char_traits<char>,  std::allocator<char> >, std::__cxx11::basic_string<char,  std::char_traits<char>, std::allocator<char> >)'
Main.cpp:(.text+0xce): undefined reference to `Name::getName[abi:cxx11]()'
collect2: error: ld returned 1 exit status

Main.cpp的

#include <iostream>
#include "Name.h"

using namespace std;

int main(){
  Name name("Johannes", "Gustafsson");
  cout << name.getName() << endl;
}

Name.h

#ifndef NAME_H
#define NAME_H

#include <string>
using namespace std;

class Name{
private:
  string forName;
  string lastName;

public:
  Name();
  Name(string pforName, string plastName);
  void setName(string pforName, string plastName);
  string getName();
};

#endif

Name.cpp

#include "Name.h"
#include <string>

using namespace std;

Name::Name(){
  forName = "For name";
  lastName = "Last name";
}

Name::Name(string pforName, string plastName){
  forName = pforName;
  lastName = plastName;
}

void Name::setName(string pforName, string plastName){
  forName = pforName;
  lastName = plastName;
}

string Name::getName(){
  return forName + " " + lastName;
}

0 个答案:

没有答案