构造函数和 g++ 编译配方的问题
problem with constructors and g++ compile recipe
我正在使用适用于 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 文件放在一个文件中,它 运行s Aok 打印构造函数和析构函数 cout 就可以了。
有人可以帮忙吗?代码如下。
我曾经 运行 一起编译配方: g++ -std=c++0x classes.h classes.cpp mainiz.cpp
classes.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
classes.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
已声明但从未实施。
如果您不想实现它们,请将它们设为纯虚拟的,如
class Object {
virtual Object* clone() = 0;
};
为对象 class 创建析构函数 "virtual" 你会得到另一个关于 Object::clone 和 Object::toString.
未定义引用的错误
您可以尝试@Igor 建议的方法,但是您当前的 mainiz.cpp 代码将无法运行,因为 C++ 不允许 class 使用纯虚方法。
您可以试试下面的代码:
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
}
None 上面给出的解决方案是 correct.The 问题在我的编译中 recipe.These 函数在 C++11 之后开始存在所以如果你使用类似的东西你的编译食谱应该是:
g++ -g -std=c++11 -o executable file.cpp main.cpp
我正在使用适用于 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 文件放在一个文件中,它 运行s Aok 打印构造函数和析构函数 cout 就可以了。 有人可以帮忙吗?代码如下。 我曾经 运行 一起编译配方: g++ -std=c++0x classes.h classes.cpp mainiz.cpp
classes.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
classes.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
已声明但从未实施。
如果您不想实现它们,请将它们设为纯虚拟的,如
class Object {
virtual Object* clone() = 0;
};
为对象 class 创建析构函数 "virtual" 你会得到另一个关于 Object::clone 和 Object::toString.
未定义引用的错误您可以尝试@Igor 建议的方法,但是您当前的 mainiz.cpp 代码将无法运行,因为 C++ 不允许 class 使用纯虚方法。
您可以试试下面的代码:
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
}
None 上面给出的解决方案是 correct.The 问题在我的编译中 recipe.These 函数在 C++11 之后开始存在所以如果你使用类似的东西你的编译食谱应该是:
g++ -g -std=c++11 -o executable file.cpp main.cpp