c++:error qualification in declaration of '....'

本文详细解释了C++中命名空间的作用及使用方法,并通过实例对比了使用与未使用命名空间的区别,帮助读者理解如何避免函数名冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上述问题是命名空间使用不当,产生的问题。

在通常情况下,建立一个工程都会包含头文件和函数具体的定义。在一些情况下,会使用命名空间。其实在刚开始学c++的时候,相信小伙伴们就会经常使用下面这句话:

using namespace std;

这就是使用命名空间,使用cout的时候可以直接按照下面的使用方法:

cout << "hello c++" << endl;
//而不是使用
std::cout << "hello c++" << std::endl;

在使用OpenCV也会使用

using namespace cv;

因为c++的第三方库特别多,然后对应函数名也会有相同,这时候程序就会很疑惑,运行哪个库中的函数,所以加上命名空间相当于给程序一个门牌号,找到对应的门牌号再找具体的函数。

O(∩_∩)O哈哈~其实自己写工程的时候,也会使用命名空间。通常在头文件中的定义形式如下:

namespace(
    Function();
)

在.cpp文件中怎么写具体的函数的定义呢?

#include "...."
namespace (
    Function()
    {
        ....
    }
)

在函数定义的时候不能写成下面的格式:

#include "...."
namespace::Function()
    {
        ....
    }

否则就会把报标题类似的错误O(∩_∩)O哈哈~周六(^o^)/~

a.cpp:35:5: error: extra qualification ‘Stud::’ on member ‘sum’ [-fpermissive] 35 | int Stud::sum=0; | ^~~~ a.cpp:35:15: error:int Stud::sum’ conflicts with a previous declaration 35 | int Stud::sum=0; | ^ a.cpp:15:19: note: previous declaration ‘double Stud::sum’ 15 | static double sum; | ^~~ a.cpp:36:5: error: extra qualification ‘Stud::’ on member ‘num’ [-fpermissive] 36 | int Stud::num=0; | ^~~~ a.cpp:45:2: error: expected ‘}’ at end of input 45 | } | ^ a.cpp:9:12: note: to match this ‘{’ 9 | class Stud { | ^ a.cpp: In member function ‘void Stud::print()’: a.cpp:28:24: error: expected primary-expression before ‘,’ token 28 | cout <<number<<, name<<, score<< endl; | ^ a.cpp:28:32: error: expected primary-expression before ‘,’ token 28 | cout <<number<<, name<<, score<< endl; | ^ a.cpp:28:39: error: invalid operands of types ‘float’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’ 28 | cout <<number<<, name<<, score<< endl; | ~~~~~^~~~~~~ a.cpp: In member function ‘int Stud::main()’: a.cpp:40:8: error: ‘class Stud’ has no member named ‘disp’ 40 | s1.disp(); | ^~~~ a.cpp:41:8: error: ‘class Stud’ has no member named ‘disp’ 41 | s2.disp(); | ^~~~ a.cpp:42:8: error: ‘class Stud’ has no member named ‘disp’ 42 | s3.disp(); | ^~~~ a.cpp:43:25: error: ‘avg’ is not a member of ‘Stud’ 43 | cout<<"avg="<<Stud::avg()<<endl; | ^~~ a.cpp: At global scope: a.cpp:45:2: error: expected unqualified-id at end of input 45 | } | ^
最新发布
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值