缺少显式类型C++(假定为"int")

C++ explicit type is missing('int' assumed)

本文关键字:int 类型 C++      更新时间:2023-10-16

嗨,我的 c++ 代码中有错误。我有 2 个.cpp文件和 1 个 .h 文件,我试图从头文件中访问 5 个字符串和 1 个 int,但我收到一个错误,说"缺少显式类型(假设为'int')。

我还有其他一些错误,它们是:缺少类型说明符,商店::项目重新定义;不同的基本类型,重载函数仅在返回类型上有所不同,声明不兼容。

这是我的文件:

用户选择.h

#include <iostream>
#include <string>
using namespace std;
#ifndef USERCHOICE_H
#define USERCHOICE_H
class Shops
{
public:
    double Items(string, string, string, string, string, int);
    int main()
    {
        std::cout << newItem1;
    }
private:
    string newItem1;
    string newItem2;
    string newItem3;
    string newItem4;
    string newItem5;
    int newItems;
};
#endif

物品.cpp

#include "UserChoice.h"
Shops::Items(string Item1, string Item2, string Item3, string Item4, string Item5, int     Items)
{
    newItem1 = Item1;
    newItem2 = Item2;
    newItem3 = Item3;
    newItem4 = Item4;
    newItem5 = Item5;
    newItems = Items;
}

资料来源.cpp

#include "UserChoice.h";
#include <string>
int main()
{
    string Item1;
    string Item2;
    string Item3;
    string Item4;
    string Item5;
    int items;
    std::cout << "What what you like? Chicken, Meat, Fish, Carrot or Apple?n";
    std::cin >> Item1;
    std::cout << "nAnything else?n";
    std::cin >> Item2;
    if(Item2 == "nothing else")
    {
    }
    std::cout << "nAnything else?n";
    std::cin >> Item3;
    std::cout << "nAnything else?n";
    std::cin >> Item4;
    std::cout << "nAnything else?n";
    std::cin >> Item5;
    std::cout << "nAnything else?n";
}

错误线

Shops::Items(string Item1, string Item2, string Item3, string Item4, string Item5, int Items)

所有代码尚未完成,因此我希望您能帮助我找到并修复这些错误。提前感谢,如果您需要更多信息,请问我。

您在 Shops::Items 的实现文件 (cpp) 中缺少返回类型,根据您在头文件中的内容,这将是一个双精度。您遇到的其他错误很可能是相关的。

在您的类中拥有一个名为 main 的方法有点令人不安,因为它通常是用于程序入口点的函数名称。

您在 Items.cpp 中的定义中缺少返回类型:

double Shops::Items(string Item1, string Item2, string Item3, string Item4, string Item5, int Items)
{
    //...
}

您还需要为 Shops::Items 和类的 main 函数返回一些值。

关于命名:在类中复制"正常"main函数看起来很奇怪,并且有一个与类命名完全相同的参数看起来也很奇怪。它确实有效,但我会在代码审查 FWIW 中标记它。:)