构造函数和 G++ 编译配方的问题

problem with constructors and g++ compile recipe

本文关键字:问题 编译 G++ 构造函数      更新时间:2023-10-16

我正在使用应用于 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::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
}

声明Object::cloneObject::toString,但从未实现。

如果你想让它们不实现,把它们变成纯虚拟的,如

class Object {
virtual Object* clone() = 0;
};

上面给出的解决方案都不是正确的。问题出在我的编译配方中。这些函数在 C++11 之后开始存在,所以如果你使用类似的东西,你的编译配方应该是:

g++ -g -std=c++11 -o 可执行文件.cpp main.cpp