g ++错误:字段类型不完整

时间:2022-06-06 22:38:52

I'm trying to work with inner classes. I need to call get function from the nested class. What am I doing wrong? Thank you for your time!

我正在尝试使用内部类。我需要从嵌套类中调用get函数。我究竟做错了什么?感谢您的时间!

   class Discriminant
{
private:
    float d;
public:
    void calcDiscr(int temp_a,int temp_b,int temp_c)
    {
        d = (temp_b^2)-4*temp_a*temp_c;
    }
    float get_d()
    {
        return d;
    }
    class Result
    {
    private:
        float x1,x2;
    public:
        Discriminant tempObject1;//here comes the error
        void calcResult(int temp_a,int temp_b,int temp_c)
        {
            cout<<"object's d = "<<tempObject1.get_d();
            x1 = (-temp_b+sqrt(tempObject1.get_d()))/2*temp_a;
            x2 = (-temp_b-sqrt(tempObject1.get_d()))/2*temp_a;
        }
        void displayResult()
        {
            cout<<endl<<"x1 = "<<x1;
            cout<<endl<<"x2 = "<<x2;
        }
    };
};

2 个解决方案

#1


0  

When the compiler reads the

当编译器读取时

Discriminant tempObject1;//here comes the error

line, the definition of Discriminant has not been fully parsed (hence the incomplete type error); which only ends with the final ;, closing the class Discriminant statement.

在线,判别式的定义尚未完全解析(因此不完整的类型错误);只有最终结束;结束判别语句。

Theoretical solutions that do not require Discriminant to be a fully-defined type are to make tempObject1 either:

不要求判别式为完全定义类型的理论解决方案是使tempObject1:

  1. Discriminant*
  2. Discriminant&

Of which only solution #1 is feasible.

其中只有解决方案#1是可行的。

#2


0  

That is your code, that doesn't compile:

那是你的代码,不编译:

class Discriminant
{
    // etc.

    class Result
    {
        // etc.
        Discriminant tempObject1; //here comes the error
    };
};

The problem is, like reported by haavee, that Discriminant is not fulled parsed (and thus incomplete) when you try to make it a member of Result.

问题是,正如haavee所报道的那样,当你试图使其成为Result的成员时,判别式不会被解析(因而不完整)。

One solution is to make tempObject1 a pointer, instead of a value.

一种解决方案是使tempObject1成为指针而不是值。

Another solution is to define Result after Discriminant (I assume you want to keep the IMHO awful inner class style). The code becomes:

另一个解决方案是在判别后定义Result(我假设你想保持IMHO糟糕的内部类风格)。代码变成:

class Discriminant
{
    // etc.

    class Result;
};

class Discriminant::Result
{
    // etc.
    Discriminant tempObject1; // No error!
};

This should solve your compilation problem.

这应该可以解决您的编译问题。

P.S.: I missed the first part of your question:

P.S。:我错过了你问题的第一部分:

I'm trying to work with inner classes. I need to call get function from the nested class.

我正在尝试使用内部类。我需要从嵌套类中调用get函数。

I hope your using nested classes is not an attempt to use Java's bizarre version's.

我希望您使用嵌套类不是尝试使用Java的奇怪版本。

If you expect your inner class to have a pointer/reference to the outer class like in Java, you will be disappointed. This is a Java peculiarity. C++ and C# don't implement this bizarre feature. You'll have to pass the pointer manually.

如果你希望你的内部类有一个像Java一样的外部类的指针/引用,你会感到很失望。这是Java的特色。 C ++和C#没有实现这个奇怪的功能。你必须手动传递指针。

#1


0  

When the compiler reads the

当编译器读取时

Discriminant tempObject1;//here comes the error

line, the definition of Discriminant has not been fully parsed (hence the incomplete type error); which only ends with the final ;, closing the class Discriminant statement.

在线,判别式的定义尚未完全解析(因此不完整的类型错误);只有最终结束;结束判别语句。

Theoretical solutions that do not require Discriminant to be a fully-defined type are to make tempObject1 either:

不要求判别式为完全定义类型的理论解决方案是使tempObject1:

  1. Discriminant*
  2. Discriminant&

Of which only solution #1 is feasible.

其中只有解决方案#1是可行的。

#2


0  

That is your code, that doesn't compile:

那是你的代码,不编译:

class Discriminant
{
    // etc.

    class Result
    {
        // etc.
        Discriminant tempObject1; //here comes the error
    };
};

The problem is, like reported by haavee, that Discriminant is not fulled parsed (and thus incomplete) when you try to make it a member of Result.

问题是,正如haavee所报道的那样,当你试图使其成为Result的成员时,判别式不会被解析(因而不完整)。

One solution is to make tempObject1 a pointer, instead of a value.

一种解决方案是使tempObject1成为指针而不是值。

Another solution is to define Result after Discriminant (I assume you want to keep the IMHO awful inner class style). The code becomes:

另一个解决方案是在判别后定义Result(我假设你想保持IMHO糟糕的内部类风格)。代码变成:

class Discriminant
{
    // etc.

    class Result;
};

class Discriminant::Result
{
    // etc.
    Discriminant tempObject1; // No error!
};

This should solve your compilation problem.

这应该可以解决您的编译问题。

P.S.: I missed the first part of your question:

P.S。:我错过了你问题的第一部分:

I'm trying to work with inner classes. I need to call get function from the nested class.

我正在尝试使用内部类。我需要从嵌套类中调用get函数。

I hope your using nested classes is not an attempt to use Java's bizarre version's.

我希望您使用嵌套类不是尝试使用Java的奇怪版本。

If you expect your inner class to have a pointer/reference to the outer class like in Java, you will be disappointed. This is a Java peculiarity. C++ and C# don't implement this bizarre feature. You'll have to pass the pointer manually.

如果你希望你的内部类有一个像Java一样的外部类的指针/引用,你会感到很失望。这是Java的特色。 C ++和C#没有实现这个奇怪的功能。你必须手动传递指针。