期望构造函数、析构函数或类型转换之前的“&”标记错误。

时间:2022-10-06 20:36:49

My files:

我的文件:

cpu_add.h

cpu_add.h

#ifndef CPU_ADD_H
#define CPU_ADD_H

double add_double_double(double a, double b) {return (a+b);}
double add_int_double(int a, double b) {return ((double)(a)+b);}
int   add_int_int(int a, int b) {return (a+b);}

#endif

I am allowed only to use the functions above. Can't use the operator '+' in my own code.

我只允许使用上面的函数。不能在我自己的代码中使用运算符+。

poly_subtype.h

poly_subtype.h

#ifndef POLY_SUBTYPE_H
#define POLY_SUBTYPE_H
#include <iostream>
#include "q3.h"
#include "cpu_add.h"
using std::cout;
using std::endl;

//Deriving classes definition
class IntClass;
class DoubleClass;

//The Virtual Number Class. IntClass and FloatClass will derive from this class.
class Number {
    public:
        //return a Number object that's the results of x+this, when x is DoubleClass
        virtual Number& addDouble(DoubleClass& x) = 0;

        //return a Number object that's the results of x+this, when x is IntClass
        virtual Number& addInt(IntClass& x) = 0;

        //return a Number object that's the results of x+this, when x is either
        //IntClass or DoubleClass
        virtual Number& operator+(Number& x) = 0;

        //Print the number stored in the object
        virtual void print_number() = 0;        
};

class IntClass : public Number {
    private:
        int my_number;
    public:
        //Constructor
        IntClass(int n):my_number(n) {}

        //returns the number stored in the object
        int get_number()  {return my_number;}

        //print the number stored in the object
        void print_number() {cout << my_number << endl;}

        //return a DoubleClass object that's the result of x+this
        Number& addDouble(DoubleClass& x);

        //return an IntClass object that's the result of x+this
        Number& addInt(IntClass& x);

        //return a Number object that's the result of x+this.
        //The actual class of the returned object depends on x.
        //If x is IntClass, then the result if IntClass.
        //If x is DoubleClass, then the results is DoubleClass.
        Number& operator+(Number& x);
};

class DoubleClass : public Number {
    private:
        double my_number;
    public:
        //Constructor
        DoubleClass(double n):my_number(n) {}

        //returns the number stored in the object
        double get_number()  {return my_number;}

        //Print the number stored in the object
        void print_number() {cout << my_number << endl;}

        //return a DoubleClass object that's the result of x+this
        Number& addDouble(DoubleClass& x);

        //return a DoubleClass object that's the result of x+this
        Number& addInt(IntClass& x);

        //return a DoubleClass object that's the result of x+this.
        //This should work if x is either IntClass or DoubleClass
        Number& operator+( Number& x);
};

#endif

I need to implement the next functions:

我需要实现下一个功能:

Number& IntClass::addInt(IntClass& x);
Number& IntClass::addDouble(DoubleClass& x);
Number& IntClass::operator+(Number& x);
Number& DoubleClass::addInt(IntClass& x);
Number& DoubleClass::addDouble(DoubleClass& x);
Number& DoubleClass::operator+(Number& x);

So for example in my the q3.h I wrote:

例如在我的q3。h我写道:

#ifndef Q3_H_
#define Q3_H_

#include "cpu_add.h"
#include "poly_subtype.h"

Number& IntClass::addInt(IntClass& x){
    IntClass num(add_int_int(my_number, x.get_number()));
    return num;
}
#endif /* Q3_H_ */

and I get the next error:

我得到了下一个错误

expected constructor, destructor, or type conversion before '&' token   q3.h    ‪/pl5‬  line 48 C/C++ Problem

What does it mean?

这是什么意思?

Thanks.

谢谢。

4 个解决方案

#1


1  

This is caused because you put all your code in headers (maybe thinking the Java way?).

这是因为您将所有代码都放在了header中(可能会考虑Java方法?)

When you include poly_subtype.h it includes q3.h which then includes poly_subtype.h in an attempt to define various classes. Due to your include guards the second include has no effect. Then when the compiler attempts to compile q3.h it has no idea what the Number class is, and generates that error.

当你包括poly_subtype。它包括第三季。然后包括poly_subtype。h试图定义不同的类。由于你的包括警卫,第二包括没有影响。然后当编译器尝试编译q3时。它不知道Number类是什么,并生成这个错误。

The fix to this is to put your method implementations into source files that are only compiled once. It will prevent the nested inclusion problems. Alternately I can't see why poly_subtype.h needs to include q3.h at all. If you remove that include it might fix it.

解决这个问题的方法是将方法实现放到只编译一次的源文件中。它将防止嵌套的包含问题。另一种情况是,我看不到为什么poly_subtype。h需要包括q3。h。如果你移除它,它可能会修复它。

Also as a side note, IntClass::addInt returns a local by reference which is definitely incorrect although it may appear to work. You should instead return *this; which is the actual object being modified (I believe - it's hard to tell from your small sample).

另外,作为附加说明,IntClass::addInt以引用的方式返回一个本地,尽管它可能看起来有效,但它肯定是不正确的。相反,您应该返回*this;这是正在修改的实际对象(我相信,从您的小样本中很难看出)。

#2


2  

poly_subtype.h includes q3.h, and q3.h includes poly_subtype.h. Theoretically this is allowed, because of your multiple-inclusion guards; but I don't think it's a good idea :-)

poly_subtype。h包括第三季。h,第三季。h包括poly_subtype.h。理论上这是允许的,因为你有多重包容的守卫;但我不认为这是个好主意:-)

#3


1  

Its because you have circular dependency:

因为你有循环依赖性:

Sya you have the file: x.cpp

Sya您有文件:x.cpp。

X.cpp

 #include "poly_subtype.h"

Now the inheritance looks like this:

现在遗传看起来是这样的:

x.cpp
    // ---Included from x.cpp
    "poly_subtype.h"
        // ---Included from poly_subtype.h
        <iostream>               // OK
        "q3.h"
            // ---Included from q3.h
            "cpu_add.h"          // OK
            "poly_subtype.h"     // FAIL. poly_subtype.h guards kick in here.
                                 //       So no classes are defined.

                // DOUBLE FAIL
                // The class did not kick in becuase we failed to load the header file
                //
                // At this point IntClass has not been defined.
                Number& IntClass::addInt(IntClass& x){
                    IntClass num(add_int_int(my_number, x.get_number()));
                    return num;
                }
            // EXIT ---Included from q3.h

        "cpu_add.h"              // Already done. No Problem.

         class IntClass   // OOOPS. Whay to late.
                          //        declared after we need them

        // EXIT ---Included from poly_subtype.h
    // EXIT ---Included from x.cpp

The simple solution.
Only put declarations in header files. Leave all the implementation to the cpp file. Don't worry about all that inline optimization you think you are missing out on, its still going to happen with modern compilers (if the compiler wants it to happen it will).

简单的解决方案。只在头文件中添加声明。将所有实现都留给cpp文件。不要担心所有的内联优化,你认为你错过了,它仍然会发生在现代编译器(如果编译器希望它发生它会)。

In the header files forward declare any type that you are using only be reference or pointer. Only #include a file if you are deriving from it as a base or you have a member variable from the class. This should break all circular dependencies.

在头文件中,向前声明您所使用的任何类型仅是引用或指针。只有#包含一个文件,如果您是从它作为一个基础派生的,或者您有一个来自类的成员变量。这将打破所有循环依赖关系。

For simplicity one class per header file (within reason, you can bundle groups of classes if they are all very similar (use best judgment)).

为了简化每个头文件的一个类(在合理范围内,如果它们都非常相似(使用最佳判断),则可以将类分组)。

#4


0  

It is really dirty to write all your code in .h files. (there are exeptions of course, but not for a novice) .h files are headers. .cpp are implementation files where you write the implementation of functions.

把所有的代码都写在.h文件中是很脏的。(当然,有一些exeptions,但不是对新手来说).h文件是header . .cpp是实现文件的实现文件。

and like TonyK said: your circular inclusion of files lead to mayhem.

就像TonyK说的那样:你的卷宗包含的文件会导致混乱。

#1


1  

This is caused because you put all your code in headers (maybe thinking the Java way?).

这是因为您将所有代码都放在了header中(可能会考虑Java方法?)

When you include poly_subtype.h it includes q3.h which then includes poly_subtype.h in an attempt to define various classes. Due to your include guards the second include has no effect. Then when the compiler attempts to compile q3.h it has no idea what the Number class is, and generates that error.

当你包括poly_subtype。它包括第三季。然后包括poly_subtype。h试图定义不同的类。由于你的包括警卫,第二包括没有影响。然后当编译器尝试编译q3时。它不知道Number类是什么,并生成这个错误。

The fix to this is to put your method implementations into source files that are only compiled once. It will prevent the nested inclusion problems. Alternately I can't see why poly_subtype.h needs to include q3.h at all. If you remove that include it might fix it.

解决这个问题的方法是将方法实现放到只编译一次的源文件中。它将防止嵌套的包含问题。另一种情况是,我看不到为什么poly_subtype。h需要包括q3。h。如果你移除它,它可能会修复它。

Also as a side note, IntClass::addInt returns a local by reference which is definitely incorrect although it may appear to work. You should instead return *this; which is the actual object being modified (I believe - it's hard to tell from your small sample).

另外,作为附加说明,IntClass::addInt以引用的方式返回一个本地,尽管它可能看起来有效,但它肯定是不正确的。相反,您应该返回*this;这是正在修改的实际对象(我相信,从您的小样本中很难看出)。

#2


2  

poly_subtype.h includes q3.h, and q3.h includes poly_subtype.h. Theoretically this is allowed, because of your multiple-inclusion guards; but I don't think it's a good idea :-)

poly_subtype。h包括第三季。h,第三季。h包括poly_subtype.h。理论上这是允许的,因为你有多重包容的守卫;但我不认为这是个好主意:-)

#3


1  

Its because you have circular dependency:

因为你有循环依赖性:

Sya you have the file: x.cpp

Sya您有文件:x.cpp。

X.cpp

 #include "poly_subtype.h"

Now the inheritance looks like this:

现在遗传看起来是这样的:

x.cpp
    // ---Included from x.cpp
    "poly_subtype.h"
        // ---Included from poly_subtype.h
        <iostream>               // OK
        "q3.h"
            // ---Included from q3.h
            "cpu_add.h"          // OK
            "poly_subtype.h"     // FAIL. poly_subtype.h guards kick in here.
                                 //       So no classes are defined.

                // DOUBLE FAIL
                // The class did not kick in becuase we failed to load the header file
                //
                // At this point IntClass has not been defined.
                Number& IntClass::addInt(IntClass& x){
                    IntClass num(add_int_int(my_number, x.get_number()));
                    return num;
                }
            // EXIT ---Included from q3.h

        "cpu_add.h"              // Already done. No Problem.

         class IntClass   // OOOPS. Whay to late.
                          //        declared after we need them

        // EXIT ---Included from poly_subtype.h
    // EXIT ---Included from x.cpp

The simple solution.
Only put declarations in header files. Leave all the implementation to the cpp file. Don't worry about all that inline optimization you think you are missing out on, its still going to happen with modern compilers (if the compiler wants it to happen it will).

简单的解决方案。只在头文件中添加声明。将所有实现都留给cpp文件。不要担心所有的内联优化,你认为你错过了,它仍然会发生在现代编译器(如果编译器希望它发生它会)。

In the header files forward declare any type that you are using only be reference or pointer. Only #include a file if you are deriving from it as a base or you have a member variable from the class. This should break all circular dependencies.

在头文件中,向前声明您所使用的任何类型仅是引用或指针。只有#包含一个文件,如果您是从它作为一个基础派生的,或者您有一个来自类的成员变量。这将打破所有循环依赖关系。

For simplicity one class per header file (within reason, you can bundle groups of classes if they are all very similar (use best judgment)).

为了简化每个头文件的一个类(在合理范围内,如果它们都非常相似(使用最佳判断),则可以将类分组)。

#4


0  

It is really dirty to write all your code in .h files. (there are exeptions of course, but not for a novice) .h files are headers. .cpp are implementation files where you write the implementation of functions.

把所有的代码都写在.h文件中是很脏的。(当然,有一些exeptions,但不是对新手来说).h文件是header . .cpp是实现文件的实现文件。

and like TonyK said: your circular inclusion of files lead to mayhem.

就像TonyK说的那样:你的卷宗包含的文件会导致混乱。