小编Xu *_*Hui的帖子

对`vtable的奇怪未定义引用

我困"undefined reference to vtable..."了一整天。

其实我已经看到很多答案 "undefined reference to vtable..."

例如:

未定义对vtable“交易”的引用

未定义对vtable的引用

https://gcc.gnu.org/faq.html#vtables

未定义对vtable的引用

有些人的问题是忘记编写虚拟函数,而其他人则忘记了将.cpp文件添加到生成目录。但是我想我已经注意到了。

我要执行以下步骤:

  • 我要编译类A_1A_2转到共享库libAA_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)

c++ g++

12
推荐指数
1
解决办法
182
查看次数

代码在存在时如何运行覆盖不明确的功能?

当存在覆盖不明确的函数时,我无法完全理解代码结果。

我有一个库libMy,其中包含两个类AB.

代码显示如下

// 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)

c++ linker compilation g++ ambiguous

5
推荐指数
1
解决办法
100
查看次数

可以根据c ++中的模板args更改类成员吗

我希望可以根据模板参数更改类成员。我想要类似的东西

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++ c++98

4
推荐指数
1
解决办法
90
查看次数

为什么在 PIMPL 中不能访问实现类的常量函数?

我想在 C++ 中尝试使用 PIMPL。

就我而言,我正在使用operator()访问私人成员。

接口类A和实现类AImpl都有operator() constoperator()

代码如下所示:

#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)

c++ constants pimpl

2
推荐指数
1
解决办法
82
查看次数

标签 统计

c++ ×4

g++ ×2

ambiguous ×1

c++98 ×1

compilation ×1

constants ×1

linker ×1

pimpl ×1