在c++11编译器中有一个额外的限定错误

Having problems with an extra qualification error in c++11 compiler

本文关键字:错误 c++11 编译器 有一个      更新时间:2023-10-16

我是一名c++11学生,我遇到了一个额外的资格错误。

我在一个。h文件中声明了一个类,在一个单独的。cpp文件中实现了一个布尔函数。

类定义如下:

class Order{
      std::string customer, product;
      std::vector<std::string> itemList;
      bool validName(std::string name);
      bool isCustomerName(std::string name);
      bool isProductName(std::string name);
      bool isItemName(std::string name);
public:
      Order(std::vector<std::string> line);
      void print(){
      void graph(std::ofstream os);
};//class Order

所有的函数都在一个单独的cpp文件中实现,并且我以以下方式定义了所有函数的作用域:

Order::Order(std::vector<std::string> line){

bool Order::isCustomerName(std::string name){

当我尝试编译cpp文件时,出现以下错误:

error: extra qualification ‘Order::’ on member ‘Order’ [-fpermissive]

查找后,这似乎是与在同一函数的类定义中使用作用域操作符或某种双重使用作用域操作符有关的错误。

我没有将cpp文件中的实现封装在单独的命名空间中,并且只包含了cpp文件的相应.h文件。有没有人能给我一点提示,告诉我解决这个问题的方向?

感谢

这是cpp文件的顶部:

#include <fstream> 
#include <iostream> 
#include <vector> 
#include <string> 
#include "order.h" 

这是来自同一个cpp的一个示例函数:

bool Order::isProductName(std::string name){ 
    if (name.size() > 0 && isalpha(name[0])) 
        return true; 
    return false; } 

上面列出的类定义实际上是类Order. h中的所有内容。

.h的顶部是:

#pragma once 
#include <fstream> 
#include <iostream> 
#include <vector> 
#include <string> 
#include "util.h"

在你的类中有这样一行:

 void print(){

我相信你是指

 void print();

由于c++编译的方式,当您说#include "order.h"时,编译器实际上是将order.h的内容复制并粘贴到cpp文件中。因此,它看到您已经为print打开了这个函数定义,并在成员函数print (gcc扩展)中声明了一些局部函数,然后您最终在标记为};//class Order的行关闭了该函数。这看起来像是类定义的结束,但实际上是函数的结束。后面在cpp文件中的函数定义被视为在类体内部,这会混淆编译器。