未解析的外部符号1和b/w虚与纯虚的区别
Unresolved external symbol 1 and the difference b/w virtual and pure virtual?
这可能出了什么问题?我们在哪里用纯virtual func() = 0;
?此外,在一个virtual
命令下是否可以使用不同的功能?我的意思是,像rotate
,我可以写成move()
吗?努力掌握多态性。
using namespace std;
class shape
{
public:
virtual void rotate();
};
class triangle : public shape
{
public:
void rotate()
{
cout << "in triangle";
}
};
class line : public shape
{
public:
void rotate()
{
cout << "in line";
}
};
class circle : public shape
{
public:
void rotate()
{
cout << "in circle";
}
};
int main()
{
shape s;
triangle t;
circle c;
line l;
shape* ptr;
ptr = &s;
ptr->rotate();
ptr = &t;
ptr->rotate();
ptr = &l;
ptr->rotate();
system("PAUSE");
return 0;
}
error: LNK 1120: 1 unresolved externals error: LNK 2001: unresolved external symbol "public: virtual void_thiscall shape::rotate(void)"(?rotate@shape@@UAEXXZ)
如果你想使用"cout",你必须包含<iostream>
头文件。
虚纯函数是一个接口,因此如果不在派生类中实现派生类,就不能实例化派生类。
必须在基类
中实现rotate函数#include <iostream>
using namespace std;
class shape
{
public:
virtual void rotate()
{
cout << "in shape";
}
};
class triangle:public shape
{
public:
void rotate()
{
cout << "in triangle";
}
};
class line : public shape
{
public:
void rotate()
{
cout << "in line";
}
};
class circle : public shape
{
public:
void rotate()
{
cout << "in circle";
}
};
int main()
{
shape s;
triangle t;
circle c;
line l;
shape *ptr;
ptr = &s;
ptr->rotate();
ptr = &t;
ptr->rotate();
ptr = &l;
ptr->rotate();
system("PAUSE");
return 0;
}
由于您将shape::rotate
方法声明为虚方法而不是纯虚方法,因此链接器正在寻找shape::rotate
的实现。
存在两种解:
1)通过附加= 0
,使shape::rotate
a 为纯虚函数。2)创建空的shape::rotate
函数。
在Shape声明中,使用如下:
virtual void rotate() = 0;
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 在 typedef 内部使用 const 关键字和在 typedef 外部使用 const 关键字之间有区别吗?
- C++/CLI 和 C#/VB 与不安全和外部有什么区别?
- 外部"C"与外部的区别
- 在矢量的尖括号内使用指针符号与在外部使用指针符号有什么区别
- 有什么区别:在命名空间 yyy 外部和内部使用 namespae xxx?
- C 和 C++ 中的外部变量有什么区别
- 在内部和外部与调试器之间运行有什么区别
- 如何区别外部硬盘和内部硬盘
- 模板化模式对象和外部密码对象有什么区别?
- 外部"C"和简单的外部之间的区别
- 相同的外部结构只有一个功能之间的区别
- 这些外部声明在C++有什么区别
- 在匿名命名空间内部和外部定义静态函数有什么区别吗?
- 未解析的外部符号1和b/w虚与纯虚的区别
- 在类定义内部和外部定义成员函数之间有区别吗
- 函数内部和外部静态变量的区别
- 在类内部或外部重载操作符有什么区别?