【发布时间】:2018-04-30 12:57:02
【问题描述】:
我有这个烦人的错误; 未定义对 Shape::Shape(...)、Shape::getName(...)、Shape::getAge(...) 的引用
我的 Main.cpp 是这个
#include <iostream>
#include <string>
#include "Bit.h"
using namespace std;
int main()
{
//simple assignment
string name;
int age;
cout<<"enter name: ";
cin>>name;
cout<<"enter age: ";
cin>>age;
Shape sh(name,age); //class declaration (i think here is the problem)
cout<<endl<<"name: "<<sh.getName();
cout<<endl<<"age: "<<sh.getAge();
return 0;
}
这是 Bit.h 标头
#include <iostream>
#include <string>
using namespace std;
#ifndef BIT_H
#define BIT_H
//creating class
class Shape{
string newName;
int newAge;
public:
//Default Constructor
Shape();
//Overload Constructor
Shape(string n,int a);
//Destructor
~Shape();
//Accessor Functions
string getName();
int getAge();
};
最后,这是 Bit.cpp
#include "Bit.h"
//constructors and destructor
Shape::Shape(){
newName="";
newAge=0;
}
Shape::Shape(string n, int a){
newName=name;
newAge=age;
}
Shape::~Shape(){
}
string Shape::getName(){
return newName;
}
//getters
int Shape::getAge(){
return newAge;
}
我了解,这可能是一个非常简单的问题/错误,但我已经挣扎了大约 2 个小时。 我想错误是在声明 od "sh" 对象中,即使我声明它像这样 "Shape sh();"或“Shape sh;”,仍然有错误。 谢谢
编辑。 GNU GCC 编译器 编辑2。使用代码块(抱歉忘记了所有这些)
【问题讨论】:
-
你如何编译这个?
-
对不起,我没有在上面添加这个。 GNU GCC 编译器和编译目标是 main.cpp
-
你需要编译和链接每个cpp文件,而不仅仅是
main.cpp -
文件中有大量错别字或错误。 (看起来有些已经修复了。)一旦我修复了这些,它就为我正确编译和运行了。
标签: c++ function class object undefined