在')'令牌之前预期的不合格身份?

时间:2016-05-14 04:10:38

标签: c++ constructor

我想创建一个基类,并在默认构造函数中使用初始化列表获取错误。以下是我得到的错误:

giasuc.cpp:3:8: error: expected unqualified-id before ')' token
 GiaSuc(): ten(""), soConSinh(0), soLitSua(0) {
        ^
giasuc.cpp:6:14: error: expected ')' before '&' token
 GiaSuc(GiaSuc& mGiasuc): ten(mGiasuc.ten), soConSinh(mGiasuc.soConSinh), soLitS
ua(mGiasuc.soLitSua) {
              ^
giasuc.cpp:9:20: error: expected ')' before '&' token
 GiaSuc(std::string &mTen, short mSoCon = 0, short mLitSua = 0): ten(mTen), soCo
nSinh(mSoCon), soLitSua(mLitSua) {
                    ^

这是我正在建设的课程( giasuc.h ):

#ifndef GIA_SUC_H
#define GIA_SUC_H

#include <iostream>
#include <string>

class GiaSuc {
protected:
    std::string ten;
    short soConSinh;
    short soLitSua;

public:
    GiaSuc();
    GiaSuc(GiaSuc& mGiasuc);
    GiaSuc(std::string &mTen, short mSoCon, short mLitSua);
    virtual void keu() = 0;
    virtual ~GiaSuc() = 0;
};

#endif // GIA_SUC_H

giasuc.cpp

#include "giasuc.h"
#include <string>
GiaSuc(): ten(""), soConSinh(0), soLitSua(0) {
}

GiaSuc(GiaSuc& mGiasuc): ten(mGiasuc.ten), soConSinh(mGiasuc.soConSinh), soLitSua(mGiasuc.soLitSua) {
}

GiaSuc(std::string &mTen, short mSoCon = 0, short mLitSua = 0): ten(mTen), soConSinh(mSoCon), soLitSua(mLitSua) {
}

我正在使用MinGW g ++(GCC)4.9.3,并用以下内容进行编译:

g++ -Wall -Wpedantic -Weffc++ -ansi -c giasuc.cpp

3 个答案:

答案 0 :(得分:1)

在.cpp文件中,构造函数不应该是convert -layers dispose input.gif output.gif。它应该是GiaSuc()。构造函数需要通过类名限定。

GiaSuc::GiaSuc()

答案 1 :(得分:1)

在类定义之外定义类的成员函数时,需要使用作用域运算符#include "giasuc.h" #include <string> GiaSuc::GiaSuc(): ten(""), soConSinh(0), soLitSua(0) { } GiaSuc::GiaSuc(GiaSuc& mGiasuc): ten(mGiasuc.ten), soConSinh(mGiasuc.soConSinh), soLitSua(mGiasuc.soLitSua) { } GiaSuc::GiaSuc(std::string &mTen, short mSoCon = 0, short mLitSua = 0): ten(mTen), soConSinh(mSoCon), soLitSua(mLitSua) { } 和类名来告诉编译器它们属于哪个类。

::

答案 2 :(得分:0)

您在课堂外定义功能时忘记了范围解析操作符     #include&#34; GiaSuc.h&#34;

GiaSuc::GiaSuc(): ten(""), soConSinh(0), soLitSua(0) {
}

 GiaSuc::GiaSuc(GiaSuc& mGiasuc): ten(mGiasuc.ten), soConSinh(mGiasuc.soConSinh),    soLitSua(mGiasuc.soLitSua) {
}

GiaSuc::GiaSuc(std::string &mTen, short mSoCon = 0, short mLitSua = 0): ten(mTen), soConSinh(mSoCon), soLitSua(mLitSua) {
}

http://en.cppreference.com/w/cpp/language/lookup