目录

一、C++11 常见编译问题

1.1 error: no matching function for call to

1.2 'xxxx' was not declared in this scope

1.3 ambiguous overload for 'operator='

1.4 crosses initialization of 或 error: jump to case label [-fpermissive]

1.5 无法为有抽象类型‘xxx’的对象分配内存

1.6 error: redefinition of ‘class xxx’

1.7 ‘xxx’is already declared in this scope

1.8 multiple definition of ‘xxx’

1.9 ‘xxx’does not name a type

 1.10 `error: invalid use of incomplete type`

1.11  invalid use of incomplete type

1.12 error: ‘XXX‘ was not declared in this scope

1.13 error has not been declared

1.14 base operand of '->' has non-pointer type 'const Comple

1.15 void value not ignored as it ought to be

1.16 jump to case label [-fpermissive]

1.17  error: ‘XXX‘ is not captured

1.18 error: ‘this’ was not captured for this lambda function 

1.19  错误:将‘const char*’赋值给‘char [1024]’时类型不兼容

1.20 marked override, but does not override

二、链接问题

2.1 multiple definition of

2.2  skipping incompatible

2.3 error adding symbols: File in wrong format

2.4 对'xxxxxxx'未定义的引用


一、C++11 常见编译问题

1.1 error: no matching function for call to

没有匹配的函数

1.2 'xxxx' was not declared in this scope

'xxxx' 在这个范围内没有声明

1.3 ambiguous overload for 'operator='

重载不明确

1.4 crosses initialization of 或 error: jump to case label [-fpermissive]

字面意思:交叉初始化

switch的case中有定义变量,即case中不能定义变量;

或明确case 变量的作用域,用{}明确。

1.5 无法为有抽象类型‘xxx’的对象分配内存

问题原因,继承类中没有实现纯虚函数。

1.6 error: redefinition of ‘class xxx’

头文件被同一个文件多次包含,需要加宏定义限制

#ifndef _TEST_H_
#define _TEST_H_

//。。。

#endif

把上面的代码加在头文件.h的头尾,即可避免重复定义的错误。

1.7 ‘xxx’is already declared in this scope

已在此作用域内声明

1.8 multiple definition of ‘xxx’

多次定义 ‘xxx’

1.9 ‘xxx’does not name a type

class A
{
public:
    B *b;
};
class B{
public:    
	A *a;
};
int main()
{
    return 0;
}

 报错为“error: ‘B’ does not name a type”,就是因为在A类中使用B *b之前没有声明或定义B类,如果在第一行加上一句前置声明(forward declaration)“class B;”,就不会有这样的问题了。

 1.10 `error: invalid use of incomplete type`

 编译器不知道所用的struct 或者是class的具体实现。

分析:通常出现在如下情况:假设我们有一个class some定义在some.h中,实现在some.cpp中,我们在other.cpp中要用到这个some 的方法,于是我们再other.h中声明可一个class some,并声明了要用到的方法,这样就会导致上述的问题,

解决:在other.cpp中include some.h,这样编译器就会根据该头文件找到class some的具体定义,问题也就解决了。

具体情况:

1.忘了定义头文件

2.没有引用头文件

1.11  invalid use of incomplete type

情况一:

出现错误的情况:

class A :

定义在A.h

实现在A.cpp

由于要使用A类里的方法,在B.h中声明了 class A;

原因:

编译器找不到A的具体实现

解决:

在B.cpp中 include A.h

情况二:

class cat;

struct dog {
    cat *point;

    friend bool operator<(const dog &a, const dog &b) {
        cat *cat_a = a.point;
        cat *cat_b = b.point;
        return cat_a->age < cat_b->age;
    }
}

class cat {
public:
    int age;
}

报错的原因大概是cat向前声明,但是编译器在运行friend bool operator<(const dog &a, const dog &b) 时暂时还不知道cat的具体实现方式,所以无法获得变量age的值。

1.12 error: ‘XXX‘ was not declared in this scope

错误:在作用域内,未声明’XXX’

(1)作用域不正确。

在超出变量和函数的作用域部分使用了该变量或函数。这时候得通过定义位置,要么增加声明的手段,加大变量的作用域使其包含引用位置。

(2)头文件相互包含。
在自己写项目头文件时,两个头文件相互包含(即相互#include),构成递归包含结构。

(3).cc 文件中,声明类

class 类名;

1.13 error has not been declared

 重复声明 

1.14 base operand of '->' has non-pointer type 'const Comple

->操作符前面的操作数类型不是指针类型

函数(&对象)

{
 对象名.成员     //正确方式

 对象名->成员     //编译报错  base operand of '->' has  non-pointer type .......

}

引用传递的参数应该理解为对象,而不是指针

1.15 void value not ignored as it ought to be

原因是因为,一个函数的返回值为void,但是你又把这个函数的返回值赋值给了一个具体类型的变量。

1.16 jump to case label [-fpermissive]

只要case中带变量不带括号,编译器都会报错,case 语句分支 加上 {}

1.17  error: ‘XXX‘ is not captured

C++ 出现 is not captured 错误原因是 未将参数添加到lamda函数 捕捉列表[]中

1.18 error: ‘this’ was not captured for this lambda function 

  1. [=] 按值捕获所有能捕获的变量:也可以捕获this指针

  2. 使用 [&] 按引用捕获所有能捕获的变量,也可以将其他变量单独按值捕获,例如:[&, param1, param2]

  3. 如果不使用=来捕捉,必须将this传进Lambda表达式中

1.19  错误:将‘const char*’赋值给‘char [1024]’时类型不兼容

修改方法

char testArray[1024];
string testStr = "hello";

std::strcpy(testArray, testStr.c_str);

1.20 marked override, but does not override

c++11中引入了override关键字,被override修饰的函数其派生类必须重载。

派生类如果使用override,则基类函数需要确保添加了virtual

class Object
{
public:
    Object(){};
    virtual ~Object(){};

    virtual void fun() override
    {
        std::cout << "Object::fun" << std::endl;
    }
};

class Base:public Object
{
public:
    Base(){};
    virtual ~Base(){};

    void fun();
};
void Base::fun()
{
    std::cout << "Base::fun" << std::endl;
}

int main(){
    Object* b = new Base;
    b->fun();
    return 0;
}

 1.21 C++ ‘constexpr’ needed for in-class initialization of static data membe

const static 和 static const一样,都不能在类内直接初始化非整形常量,可以修饰int,bool,char,但不能修饰其他类型(如double,float)

在c++11中,可以使用 constexpr static 或者 static constexpr 来修饰 非整形静态成员常量。

修改方法:

使用 constexpr static 修饰。

二、链接问题

2.1 multiple definition of

原因一:

问题关键是:在同一个文件里写了函数声明和定义,该文件被多个文件包含,造成函数的重定义。即因为一个.h文件被2个.cpp包含, .cpp都是单独编译成.o文件的,所以会出现错误

当多个文件包含同一个头文件时,而头文件中没有加上条件编译,就会独立的解释,然后生成每个文件生成独立的标示符。在编译器连接时,就会将工程中所有的符号整合在一起,由于,文件中有重名变量,于是就出现了重复定义的错误。 

给每一个头文件加上条件编译,避免该文件被多次引用时被多次解释

#ifndef TEST_H
#define TEST_H
……
#endif

方式二:

使用extern定义全局变量 ,所有的全局变量放入一个头文件 global.h (名字随意起,但要加条件编译)中,每一个变量前面加extern,声明一下这些变量将在其它文件中定义。 

方式三: 增加 inline定义为内联函数

原因二: .CC 文件 包含了 .cc 文件

2.2  skipping incompatible

cannot find -lxxxxx  找不到动态库

问题原因:编译器编译出的动态库版本不匹配导致,如动态库是x86-64 版本,而 可执行程序是aarch64版本

通过 file命令查看 动态库是什么版本,如 x86-64 版本

file  libxxxxx.so 

2.3 error adding symbols: File in wrong format

问题同2.2 库的格式不匹配 

2.4 对'xxxxxxx'未定义的引用

排查函数是否只声明,没有定义

参考文献:

【1】C++编译报错:does not name a type_Am最温柔的博客-CSDN博客

【2】解决C++项目编译时的multiple definition of重定义问题_你喜欢梅西吗的博客-CSDN博客

【3】解决C++中multiple definition of问题_慢慢的燃烧的博客-CSDN博客 

【4】
C++常见报错:“Multiple definition of xxx”_海滩油炸的博客-CSDN博客

【5】【C++】Lambda 表达式详解_代码被吃掉了的博客-CSDN博客_c++中的lambda表达式 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐