对 Class::Class/Function 的未定义引用(OOP 中的初学者)
Undefined reference to Class::Class/Function (Beginner in OOP)
我有这个烦人的错误; 对 Shape::Shape(...(, Shape::getName(...(, Shape::getAge(...( 的未定义引用
我的主要.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();
};
最后,这是位.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.使用代码块(很抱歉忘记了所有这些(
你可能不是在编译Bit.cpp
,而只是在编译Main.cpp
。
考虑到Bit.h
、Bit.cpp
和Main.cpp
在同一个文件夹中,你应该如何编译它:
g++ *.cpp
它仍然无法编译,因为在默认构造函数中,您正在尝试初始化name
,并且age
两者都不存在。
你可能的意思是newAge
,newName
?
在另一个构造函数中,name
和age
也不存在,你可能的意思是n
,a
?
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- c++中的oop(密码生成)
- OOP 中的单成员指针
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- OOP使用不同的结构与孩子一起构建建议
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- OOP 标识派生对象
- C++ class template
- 标识符Class_name未定义C++ oop
- 对 Class::Class/Function 的未定义引用(OOP 中的初学者)