缺少 C++ 显式类型(假设为“int”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19442659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:49:44  来源:igfitidea点击:

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

c++classheadercompiler-errorsint

提问by Matthew C

Hi I have an error in my c++ code. I have 2 .cpp files and 1 .h file, Im trying to access 5 strings and 1 int from the header file but I get an error that says "explicit type is missing('int' assumed).

嗨,我的 C++ 代码中有错误。我有 2 个 .cpp 文件和 1 个 .h 文件,我试图从头文件中访问 5 个字符串和 1 个 int,但我收到一条错误消息,指出“缺少显式类型(假设为‘int’)。

I have some other errors too which are: Missing type specifier, Shops::Items redefinition; different basic types, Overloaded function differs only by return type and declaration is incompatible.

我也有其他一些错误,它们是:缺少类型说明符,Shops::Items 重新定义;不同的基本类型,重载函数的区别仅在于返回类型和声明不兼容。

Here are my files:

这是我的文件:

UserChoice.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

Items.cpp

项目.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;
}

Source.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";
}

Error line

错误行

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

All the code isn't finished yet so I hope you can help me find and fix these errors. Thanks in advance and if you need anymore info just ask me.

所有代码还没有完成,所以我希望你能帮助我找到并修复这些错误。提前致谢,如果您需要更多信息,请询问我。

回答by Montdidier

You are missing the return type in the implementation file (cpp) for Shops::Items which would be a double on the basis of what you have your in header file. The other errors you have are very likely related.

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

It is a little disconcerting having a method named main within your class as it's normally a function name used for your program entry point.

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

回答by Daniel Frey

You are missing the return type in the definition in Items.cpp:

您在以下定义中缺少返回类型Items.cpp

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

You also need to return some value, both for Shops::Itemsand for the class' mainfunction.

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

Regarding the naming: It looks weird to have the "normal" mainfunction replicated inside the class and it also looks weird to have a parameter named exactly like the class is named. It does in fact work, but I would flag it in a code review FWIW. :)

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