我困"undefined reference to vtable..."了一整天。
其实我已经看到很多答案 "undefined reference to vtable..."
例如:
https://gcc.gnu.org/faq.html#vtables
有些人的问题是忘记编写虚拟函数,而其他人则忘记了将.cpp文件添加到生成目录。但是我想我已经注意到了。
A_1并A_2转到共享库libA。A_2源自A_1。A_1.h
#ifndef include_A_1_h
#define include_A_1_h
class A_1
{
public:
A_1() {}
virtual ~A_1() {} // not pure-virtual function has defined
virtual void print() = 0;
private:
};
#endif
Run Code Online (Sandbox Code Playgroud)
A_2.h
#ifndef include_A_2_h
#define include_A_2_h
#include "A_1.h"
class A_2 : public A_1
{
public:
A_2() {}
~A_2() {}
virtual void print();
private:
}; …Run Code Online (Sandbox Code Playgroud) 当存在覆盖不明确的函数时,我无法完全理解代码结果。
我有一个库libMy,其中包含两个类A和B.
代码显示如下
// A.h
#ifndef included_A_h
#define included_A_h
class A
{
public:
void print();
};
#endif
Run Code Online (Sandbox Code Playgroud)
// A.cpp
#include "A.h"
#include <iostream>
void A::print()
{
std::cout << "A from library" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
// B.h
#ifndef included_B_h
#define included_B_h
class A;
class B
{
public:
void printA(A &a);
};
#endif
Run Code Online (Sandbox Code Playgroud)
// B.cpp
#include "B.h"
#include "A.h"
void B::printA(A &a)
{
a.print();
}
Run Code Online (Sandbox Code Playgroud)
我有两个主要功能,它们可以用库生成两个可执行文件。
可以发现 Main*.cpp 看起来很奇怪。为什么需要看起来如此在底部解释。
// MainUsingCPP.cpp
#include <iostream>
#define included_A_h
class …Run Code Online (Sandbox Code Playgroud) 我希望可以根据模板参数更改类成员。我想要类似的东西
template<int value>
class MyClass
{
public:
void print()
{
// using the member
std::cout << sizeData() << std::endl;
for (int i=0;i<sizeData();i++)
{
std:cout << data[i] << std::endl;
}
}
static int sizeData()
{
#if value == 1
return 3;
#endif
#if value == 2
return 6;
#endif
}
static int sizeArray()
{
#if value == 1
return 40;
#endif
#if value == 2
return 200;
#endif
}
private:
#if value == 1
int data[3];
const static int array[40];
#endif …Run Code Online (Sandbox Code Playgroud) 我想在 C++ 中尝试使用 PIMPL。
就我而言,我正在使用operator()访问私人成员。
接口类A和实现类AImpl都有operator() const和operator()。
代码如下所示:
#include <iostream>
class AImpl
{
public:
explicit AImpl()
{
x = 0;
}
const int &operator()() const
{
std::cout << "const access in AImpl" << std::endl;
return x;
}
int &operator()()
{
std::cout << "not const access in AImpl" << std::endl;
return x;
}
private:
int x;
};
class A
{
public:
A()
{
impl = new AImpl;
}
~A()
{
delete …Run Code Online (Sandbox Code Playgroud)