‘(’ 标记错误之前的预期构造函数、析构函数或类型转换

expected constructor, destructor, or type conversion before ‘(’ token error

我正在使用 ubuntu 17.04 LTS 和 gcc 7.3 alognside CMake。不幸的是,我收到了这个错误:

[ 37%] Building CXX object source/server/Scripts/CMakeFiles/L5RP.dir/CarDealer/cardealer.cpp.o
/home/kkraujelis/Desktop/L5RP/ragemp/source/server/Scripts/CarDealer/cardealer.cpp:10:30: error: expected constructor, destructor, or type conversion before ‘(’ token
 CarDealer::CarOffer::CarOffer(CarDealer::CarDealer const* dealer, uint32_t price, std::string const& model, double fuelUsagePerKilo) {
                              ^
source/server/Scripts/CMakeFiles/L5RP.dir/build.make:182: recipe for target 'source/server/Scripts/CMakeFiles/L5RP.dir/CarDealer/cardealer.cpp.o' failed
make[2]: *** [source/server/Scripts/CMakeFiles/L5RP.dir/CarDealer/cardealer.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
CMakeFiles/Makefile2:194: recipe for target 'source/server/Scripts/CMakeFiles/L5RP.dir/all' failed
make[1]: *** [source/server/Scripts/CMakeFiles/L5RP.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

无论我在哪里阅读它,结果都是基本的语法错误或许多其他问题。可能是我瞎了,不过每次看代码好像都还好。

Header:

#pragma once

#define SIMEONS_CARDEALER   0

namespace L5RP {

    namespace Scripts {

        namespace CarDealer {

            class CarDealer;
            class CarDealerScript;

            class CarOffer {

                CarDealer const* dealer;
                uint32_t price;
                std::string model;
                double fuelUsagePerKilo;

            public:

                CarOffer(
                    CarDealer const* dealer,
                    uint32_t price,
                    std::string const& model,
                    double fuelUsagePerKilo
                );

                CarDealer const* getDealer() const;
                uint32_t getPrice() const;
                std::string const& getModelName() const;
                uint32_t getModel() const;
                double getFuelUsagePerKilo() const;

            };

            /*** Some other classes ***/

        }

    }

}

#include "Simeons/simeons.h"

来源:

#include "../scripts.h"

using namespace L5RP;
using namespace L5RP::Scripts;
using namespace L5RP::Scripts::Government;
using namespace L5RP::Scripts::CarDealer;
using namespace L5RP::Scripts::Character;
using namespace L5RP::Scripts::VehicleLogic;

CarDealer::CarOffer::CarOffer(CarDealer::CarDealer const* dealer, uint32_t price, std::string const& model, double fuelUsagePerKilo) {
    this->dealer = dealer;
    this->price = price;
    this->model = model;
    this->fuelUsagePerKilo = fuelUsagePerKilo;
}

实现函数时,必须完全限定 class 类型,包括命名空间。

通常你会简单地用适当的命名空间包围你的定义,如:

namespace L5RP {
    namespace Scripts {
        namespace CarDealer {
            CarOffer::CarOffer(CarDealer const* dealer, uint32_t price, std::string const& model, double fuelUsagePerKilo) 
            {
                this->dealer = dealer;
                this->price = price;
                this->model = model;
                this->fuelUsagePerKilo = fuelUsagePerKilo;
            }
        }
    }
}

更好的是,您可以在适当的时候在初始化列表中初始化您的成员。所以你的代码看起来像:

namespace L5RP 
{
    namespace Scripts 
    {
        namespace CarDealer 
        {
            CarOffer::CarOffer(CarDealer const* dealer, uint32_t price, std::string const& model, double fuelUsagePerKilo) 
                : dealer(dealer)
                , price(price)
                , model(model)
                , fuelUsagePerKilo(fuelUsagePerKilo)
            {
            }
        }
    }
}

https://whosebug.com/users/6752050/s-m 在评论部分修复了一个问题。

我无法将他的评论标记为答案。所以我会引用他的话:

You use two same names CarDealer for class and namespace. If you state using the namespace then you don't need to prefix class members definitions with the namespace.