繁体   English   中英

错误:“getValue 的左边必须有类/结构/联合”

[英]Error: "Left of getValue must have class/struct/union"

除了标题中的错误外,它还说A2A1是未声明的标识符,并且Result::calResult:function不接受 2 个参数,但它在其实现中接受。 最后,它说getValue左边必须有 class/struct/union。

我是 C++ 新手,正在尝试创建一个程序来比较两个数组的 3 个元素,我知道这不是最好的方法,但我正在尝试了解对象和类。

主.cpp:

#include "stdafx.h"
#include <iostream>
#include "ArrayOfThree.h"

using namespace std;

int main()
{
    ArrayOfThree A1, A2;
    Result R;
    int i = 0;
    int input;
    for (int i=0;i<2;i++)
    {   
        cin >> input;
        A1.set(i, input);

    }
    for (int i = 0; i < 2; i++)
    {
        cin >> input;
        A2.set(i, input);
    }
    R.CalResult(A1, A2);
    R.outResult();

    return 0;
}

ArrayOfThree.h

#include "stdafx.h"
#include <iostream>
#include "Result.h"
using namespace std;

class ArrayOfThree {
private:
    int a1, a2, a3;
public:
    ArrayOfThree() {
        a1 = 0; a2 = 0; a3 = 0;
    }

    void set(int index,int input) { 
        if (index == 0)
            a1 = input;
        else if (index == 1)
            a2 = input;
        else if (index == 2)
            a3 = input;
        else
            cout << "Index out of bound" << endl;
    }
    int getValue(int index) {
        if (index == 0)
            return a1;
        else if (index == 1)
            return a2;
        else if (index == 2)
            return a3;
        else
            cout << "Index out of bound" << endl;
        return 0;
    }
};

结果.h

#include "stdafx.h"
#include <iostream>

using namespace std;

class Result {

private:
    int r1=0, r2=0;
    char R;

public:
    Result() { r1 = 0; r2 = 0; R = ' '; }

    void CalResult(ArrayOfThree A1, ArrayOfThree A2)
    {
        for (int i = 0; i < 2; i++)
        {
            if (A1.getValue(i) < A2.getValue(i))
                r2++;
            else if (A1.getValue(i) > A2.getValue(i))
                r1++;
            else
                r1 = r1;
        }
        if (r1 < r2)
            R = 'B';
        else if (r1 > r2)
            R = 'A';
        else
            R = 'T';
    }

    void outResult()
    {
        if (R == 'B' || R == 'A')
            cout << "The winner is :" << R;
        else
            cout << "Its a Tie" << endl;
    }
};

看看你的代码,你的#includes有点乱。

在您的ArrayOfThree.h标头中,即使您没有在ArrayOfThree.h标头中使用Result.h中的任何代码,您也包含了Result.h

在另一方面,所述Result.h头使用ArrayOfThree其在定义的类ArrayOfThree.h首部,但是Result.h不包括该标头。

您必须包含您正在使用的所有类、函数等的头文件。 因此,要解决您的问题,添加#include "ArrayOfThree.h"到您的Result.h头和删除#include "Result.h"从你的ArrayOfThree.h头。


构建代码的更好方法是拆分类的声明和定义。 这意味着您的ArrayOfThree以及Result类都有一个 TU(翻译单元)。

例如,您的Result.h文件将变为:

#include "stdafx.h"
#include "ArrayOfThree.h"

class Result {
private:
    int r1=0, r2=0;
    char R;

public:
    Result();

    void CalResult(ArrayOfThree A1, ArrayOfThree A2);

    void outResult();
};

您的项目Result.cpp有一个新的TU ,如下所示:

#include "Result.h" // In the implementation file, you need to include the
                    // declaration of the class that you're implementing here.
#include <iostream>

using namespace std;

Result::Result() { r1 = 0; r2 = 0; R = ' '; }

void Result::CalResult(ArrayOfThree A1, ArrayOfThree A2)
{
    for (int i = 0; i < 2; i++)
    {
        if (A1.getValue(i) < A2.getValue(i))
            r2++;
        else if (A1.getValue(i) > A2.getValue(i))
            r1++;
        else
            r1 = r1;
    }
    if (r1 < r2)
        R = 'B';
    else if (r1 > r2)
        R = 'A';
    else
        R = 'T';
}

void Result::outResult()
{
    if (R == 'B' || R == 'A')
        cout << "The winner is :" << R;
    else
        cout << "Its a Tie" << endl;
}

请注意,现在,您的Result.h标头没有#include <iostream> 这是因为Result.h头不依赖于任何来自iostream ,因为是取决于零件iostream被转移到实现文件Result.cpp ,其中包括iostream头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM