“Warning:Can not find link symbol for virtual table for value XXX value”使用GCC和GDB(CodeBlock) [英] "Warning: Can't find linker symbol for virtual table for value XXX value" using GCC and GDB (CodeBlocks)
问题描述
我得到一个运行时错误(内存不能被写),在通过调试器检查后,导致在tittle中的警告。
I'm getting a runtime error ("memory can't be written") that, after inspection through the debugger, leads to the warning in the tittle.
标题如下:
componente.h:
componente.h:
#ifndef COMPONENTE_H
#define COMPONENTE_H
using namespace std;
class componente
{
int num_piezas;
int codigo;
char* proovedor;
public:
componente();
componente(int a, int b, const char* c);
virtual ~componente();
virtual void print();
};
#endif // COMPONENTE_H
complement.h实现
complement.h implementation
#include "Componente.h"
#include <string.h>
#include <iostream>
componente::componente()
{
num_piezas = 0;
codigo = 0;
strcpy(proovedor, "");
//ctor
}
componente::componente(int a = 0, int b = 0, const char* c = "")
{
num_piezas = a;
codigo = b;
strcpy(proovedor, "");
}
componente::~componente()
{
delete proovedor;//dtor
}
void componente::print()
{
cout << "Proovedor: " << proovedor << endl;
cout << "Piezas: " << num_piezas << endl;
cout << "Codigo: " << codigo << endl;
}
teclado.h
teclado.h
#ifndef TECLADO_H
#define TECLADO_H
#include "Componente.h"
class teclado : public componente
{
int teclas;
public:
teclado();
teclado(int a, int b, int c, char* d);
virtual ~teclado();
void print();
};
#endif // TECLADO_H
teclado.h实现
teclado.h implementation
#include "teclado.h"
#include <iostream>
teclado::teclado() : componente()
{
teclas = 0;//ctor
}
teclado::~teclado()
{
teclas = 0;//dtor
}
teclado::teclado(int a = 0, int b = 0, int c = 0, char* d = "") : componente(a,b,d)
{
teclas = c;
}
void teclado::print()
{
cout << "Teclas: " << teclas << endl;
}
我得到运行时错误的主要方法如下:
The main method where I get the runtime error is the following:
#include <iostream>
#include "teclado.h"
using namespace std;
int main()
{
componente a; // here I have the breakpoint where I check this warning
a.print();
return 0;
}
但是,如果不是创建一个componente teclado对象,我没有得到运行时错误。我STILL在调试期间得到警告,但程序的行为正如预期:
BUT, if instead of creating an "componente" object, I create a "teclado" object, I don't get the runtime error. I STILL get the warning during debugging, but the program behaves as expected:
#include <iostream>
#include "teclado.h"
using namespace std;
int main()
{
teclado a;
a.print();
return 0;
}
这会返回Teclas = 0 东西。
This returns "Teclas = 0" plus the "Press any key..." thing.
你有什么想法为什么链接器有这个troube吗?
Do you have any idea why the linker is having troube with this? It doesn't show up when I invoke the virtual function, but before, during construction.
推荐答案
在我调用虚函数时, :
Two errors that I can see:
strcpy(proovedor, ""); // No memory has been allocated to `proovedor` and
// it is uninitialised.
因为它是未初始化的,这可能会覆盖进程内存中的任何地方,所以可能会破坏虚拟表
As it is uninitialised this could be overwriting anywhere in the process memory, so could be corrupting the virtual table.
您可以将其更改为(在两个构造函数中):
You could change this to (in both constructors):
proovedor = strdup("");
析构函数使用不正确的 delete
$ c> proovedor :
Destructor uses incorrect delete
on proovedor
:
delete proovedor; // should be delete[] proovedor
由于这是C ++,应该考虑使用 std :: string
而不是 char *
。
As this is C++ you should considering using std::string
instead of char*
.
std :: string
,那么您需要:
-
实现一个复制构造函数和赋值运算符作为默认版本不正确如果你有一个动态分配的成员变量,或
-
使复制构造函数和赋值运算符私有,使得它们不可能被使用。
这篇关于“Warning:Can not find link symbol for virtual table for value XXX value”使用GCC和GDB(CodeBlock)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!