c++ - 构造函数和 g++ 编译配方的问题
问题描述
我正在使用适用于 C++11 的函数创建一个 cpp 程序。即使代码看起来正确并且没有语法错误,我在编译时也会收到此消息:
/tmp/cce9dpew.o: In function `Object::Object()':
classes.cpp:(.text+0xd): undefined reference to `vtable for Object'
/tmp/cce9dpew.o: In function `Object::~Object()':
classes.cpp:(.text+0x45): undefined reference to `vtable for Object'
/tmp/cce9dpew.o:(.rodata._ZTI6String[_ZTI6String]+0x10): undefined reference to `typeinfo for Object'
collect2: error: ld returned 1 exit status
我必须在这里补充一点,如果我将所有这些 .cpp 和 .h 文件放在一个中,它会运行 Aok 打印构造函数和析构函数 cout 就可以了。有人可以帮忙吗?代码如下。 编译我用来一起运行它们的配方: g++ -std=c++0x classes.h classes.cpp mainiz.cpp
类.h:
#ifndef CLASSES_H
#define CLASSES_H
#include <iostream>
#include <cstring>
using namespace std;
class Object
{
private:
int id;
public:
Object();
~Object();
void set_id(int ids);
int get_id();
void Equal(Object* bj) const;
void Identical(Object* bj) const;
virtual Object* clone();
virtual void toString();
};
class String:public Object
{
string characters;
public:
String();
~String();
void set_char(string a);
string get_char();
String* clone();
void toString();
int Length();
void Clear(string a);
string& Concat(string &a);
char At(char b);
string& UpdateAt(string a,string charact);
void Print(const string a) const;
};
#endif //CLASSES_H
类.cpp:
#include <iostream>
#include <cstring>
#include "classes.h"
using namespace std;
//FOR OBJECT CLASS
Object::Object(){ cout << "An object just got created." << endl;}
Object::~Object(){ cout << "An object just got destroyed." << endl; }
void Object::set_id(int ids) { this->id = ids; }
int Object::get_id() { return this->id;}
void Object::Equal(Object* bj) const
{
if((this->id == bj->id))
{
cout << "The objects are equal." << endl;
}
else
{
cout << "The objects are not equal." <<endl;
}
}
void Object::Identical(Object* bj) const
{
if(this==bj)
{
cout << "The objects are identical." <<endl;
}
else
{
cout << "The objects are not identical." <<endl;
}
}
//FOR STRING CLASS
String::String(){ cout << "String just created" << endl;}
String::~String(){ cout << "String to be destroyed" << endl;}
void String::set_char(string a) { this->characters = a;}
string String::get_char() { return this->characters;}
String* String::clone() { return this;}
void String::toString() {cout << "characters" << endl;}
int String::Length()
{
string a = this->characters;
return a.length();
}
void String::Clear(string a)
{
this->characters.clear();
}
string& String::Concat(string &a){ return (this->characters.append(a));}
char String::At(char b) { return (this->characters.find(b)); }
string& String::UpdateAt(string a,string charact)
{
int position=this->characters.find(charact);
return this->characters.replace(position,1,a);
}
void String::Print(const string a) const { cout << "print of string:" << a << endl; }
mainiz.cpp:
#include <iostream>
#include <cstring>
#include "classes.h"
using namespace std;
int main()
{
Object k;
Object *st = new String;
String d;
}
解决方案
将 Object 类的析构函数设为“虚拟”,您将收到另一个错误,因为未定义对 Object::clone 和 Object::toString 的引用。
您可以尝试@Igor 的建议,但您当前的mainiz.cpp代码将不起作用,因为 C++ 不允许具有纯虚拟方法的类的实例。
您可以尝试以下代码:
class Object {
virtual ~Object();
virtual Object* clone();
virtual void toString();
};
Object* Object::clone() {
// Make your implementation here
return nullptr;
}
void Object::toString() {
// Make your implementation here
}
推荐阅读
- python - 如何在python中使用BeautifulSoup在没有类名的范围内提取文本
- javascript - 通过特定键访问对象
- saml - 如何在 Okta 的属性中使用配置文件映射
- javascript - 如何在Vue JS中使用按字母顺序返回数组
- html - 如何获取html中生成的多个选择标签的ngValue
- dynamic - Pywasm 运行时错误
- ruby-on-rails - 在 Rails 上的“每个”红宝石中显示其他数据
- mysql - 在 mysql 中查找多个分组的第二高值
- powershell - PowerShell 从文本文件中提取值并导出到 csv
- wordpress - 每 10 分钟更新一次用户元数据