在使用avr-gcc进行编译时,我遇到了链接器错误,如下所示:
undefined reference to `__cxa_pure_virtual'
Run Code Online (Sandbox Code Playgroud)
我发现这个文件说明:
该
__cxa_pure_virtual函数是在调用纯虚函数时调用的错误处理程序.如果您正在编写具有纯虚函数的C++应用程序,则必须提供自己的
__cxa_pure_virtual错误处理函数.例如:
extern "C" void __cxa_pure_virtual() { while (1); }
根据建议定义此函数可以修复错误,但我想知道:
几乎是最后一步,但仍然有一些奇怪的错误......
bash-3.2$ make
g++ -Wall -c -g Myworld.cc
g++ -Wall -g solvePlanningProblem.o Position.o AStarNode.o PRM.o PRMNode.o World.o SingleCircleWorld.o Myworld.o RECTANGLE.o CIRCLE.o -o solvePlanningProblem
Undefined symbols:
"vtable for Obstacle", referenced from:
Obstacle::Obstacle()in Myworld.o
"typeinfo for Obstacle", referenced from:
typeinfo for RECTANGLEin RECTANGLE.o
typeinfo for CIRCLEin CIRCLE.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [solvePlanningProblem] Error 1
Run Code Online (Sandbox Code Playgroud)
vtable和typeinfo的含义是什么?
当我在窗口类中找到以下函数定义时,我正在浏览一个(不想命名)GUI Toolkit的源代码,它包含了Windows API:
virtual LRESULT CALLBACK wndProc (HWND, UINT, WPARAM, LPARAM) = 0;
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?如何为整数分配函数?还是分配给它NULL?如果你想在wndproc中使用函数指针,你需要这样做吗?
shared_ptr<Shape> circle(new Circle(Vec2f(0, 0), 0.1, Vec3f(1, 0, 0)));
shared_ptr<Shape> rect(new Rect2f(Vec2f(0, 0), 5.0f, 5.0f, 0,
Vec3f(1.0f, 1.0f, 0)) );
Run Code Online (Sandbox Code Playgroud)
我试图理解为什么以上不会编译.无论出于什么原因,当我尝试创建一个Rect2f实例(其中DOES继承自Shape指定shared_ptr模板参数的类时,就像Circle),我得到以下错误:
error: expected type-specifier before 'Rect2f'
error: expected ')' before 'Rect2f'
Run Code Online (Sandbox Code Playgroud)
关于这一切的一切都Circle shared_ptr很好.它没有问题; 它只是Rect2f导致问题的原因.
此外,传递给构造函数的值是有效的.
那么,这个问题会出现什么问题呢?我已经Rect.h包含了这个.为了简洁起见(并且我错过了该文件中可能影响此内容的内容),我将发布以下内容:
Rect.h
#pragma once
#include <QGLWidget>
#include <GL/glext.h>
#include <cmath>
#include <QDebug>
#include "Shape.h"
#include "Vec3f.h"
#include "rand.h"
const int DEFAULT_SQUARE_WIDTH = 5;
const int DEFAULT_SQUARE_HEIGHT = 5;
typedef enum {
RC_TOPLEFT, …Run Code Online (Sandbox Code Playgroud) 例如:
class Base {
virtual void my_function() = 0;
};
class Derived : Base {
void my_function() override;
};
Run Code Online (Sandbox Code Playgroud)
从我读到的,override关键字用于确保我们在我们覆盖的函数中具有正确的签名,并且它似乎是它的唯一用途.
但是,在纯虚函数的情况下,如果我们在Derived类(或Base类中使用不正确的签名,取决于如何看待事物)中使用不正确的签名,编译器将抛出错误.那么,在声明override结束时添加是否有任何意义Derived::my_function()?
我想去那里.但说真的,如何以"Apple"方式实现纯虚方法?您是否使用基类的协议并在这些方法上抛出异常?
我被告知要让我的课抽象:
public abstract class Airplane_Abstract
Run Code Online (Sandbox Code Playgroud)
并制作一个名为move virtual的方法
public virtual void Move()
{
//use the property to ensure that there is a valid position object
double radians = PlanePosition.Direction * (Math.PI / 180.0);
// change the x location by the x vector of the speed
PlanePosition.X_Coordinate += (int)(PlanePosition.Speed * Math.Cos(radians));
// change the y location by the y vector of the speed
PlanePosition.Y_Coordinate += (int)(PlanePosition.Speed * Math.Sin(radians));
}
Run Code Online (Sandbox Code Playgroud)
而另外4种方法应该是"纯虚拟方法".究竟是什么?
它们现在看起来都像这样:
public virtual void TurnRight()
{
// turn right relative to the …Run Code Online (Sandbox Code Playgroud) 我有一个包含纯虚函数的基类MyBase:
void PrintStartMessage() = 0
我希望每个派生类在它们的构造函数中调用它
然后我把它放在基类(MyBase)构造函数中
class MyBase
{
public:
virtual void PrintStartMessage() =0;
MyBase()
{
PrintStartMessage();
}
};
class Derived:public MyBase
{
public:
void PrintStartMessage(){
}
};
void main()
{
Derived derived;
}
Run Code Online (Sandbox Code Playgroud)
但我收到链接器错误.
this is error message :
1>------ Build started: Project: s1, Configuration: Debug Win32 ------
1>Compiling...
1>s1.cpp
1>Linking...
1>s1.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall MyBase::PrintStartMessage(void)" (?PrintStartMessage@MyBase@@UAEXXZ) referenced in function "public: __thiscall MyBase::MyBase(void)" (??0MyBase@@QAE@XZ)
1>C:\Users\Shmuelian\Documents\Visual Studio 2008\Projects\s1\Debug\s1.exe : fatal …Run Code Online (Sandbox Code Playgroud) 在C++ 98中,空指针由文字表示0(或实际上是值为零的任何常量表达式).在C++ 11中,我们更喜欢nullptr.但这对纯虚函数不起作用:
struct X
{
virtual void foo() = nullptr;
};
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?这不是很有意义吗?这只是一个疏忽吗?它会被修复吗?
在GCC上编译时,我得到错误:函数定义上的纯指定符,但是当我使用VS2005编译相同的代码时却没有.
class Dummy {
//error: pure-specifier on function-definition, VS2005 compiles
virtual void Process() = 0 {};
};
Run Code Online (Sandbox Code Playgroud)
但是当这个纯虚函数的定义不是内联时,它的工作原理是:
class Dummy
{
virtual void Process() = 0;
};
void Dummy::Process()
{} //compiles on both GCC and VS2005
Run Code Online (Sandbox Code Playgroud)
错误意味着什么?为什么我不能内联?如第二个代码示例所示,避免编译问题是否合法?
pure-virtual ×10
c++ ×8
c++11 ×2
avr-gcc ×1
c# ×1
constructor ×1
methods ×1
nullptr ×1
objective-c ×1
oop ×1
overriding ×1
shared-ptr ×1
virtual ×1
vtable ×1
winapi ×1
windows ×1