错误“在定义方法时,C++ 要求所有声明都使用类型说明符”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19800091/
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 23:08:57  来源:igfitidea点击:

Error "C++ requires a type specifier for all declarations whilst defining methods"

c++classoopheader-files

提问by Lorienas

I'm relatively new to C++ (so try and keep answers simple please!), and I can't understand why I get the error: C++ requires a type specifier for all declarations whilst defining methods.

我对 C++ 比较陌生(所以请尽量保持答案简单!),我不明白为什么会出现错误: C++ requires a type specifier for all declarations whilst defining methods.

I am trying to write a simple program to read a text file line by line, store the values into an array. However I am encountering an issue when trying to declare methods in my .cpp file. Please find code below.

我正在尝试编写一个简单的程序来逐行读取文本文件,将值存储到数组中。但是,当我尝试在我的 .cpp 文件中声明方法时遇到了一个问题。请在下面找到代码。

StringList.h

字符串列表.h

#ifndef StringListH
#define StringListH

#include <vector>
#include <string>

class StringList {
public:
     StringList();
     ~StringList();
     void PrintWords();
private:
     size_t numberOfLines;
     std::vector<std::string> str;
};

#endif

StringList.cpp

字符串列表.cpp

#include "StringList.h"
#include <fstream>
#include <istream>
#include <algorithm> // std::copy
#include <iterator>  // istream_iterator

using namespace std;

StringList::StringList()
{
    ifstream myfile("input.in");
    if (myfile.is_open())
    {
        copy(
            istream_iterator<string>(myfile),
            istream_iterator<string>(),
            back_inserter(str));
    }
    numberOfLines = str.size();
}

StringList::~StringList(){
    //Deconstructor
}

// Error Happens Here
StringList::PrintWords(){
    //Print My array
}

I have googled to no avail, I don't quite understand how to read the proper documentation for C++ yet, so I'm a little stuck. I have written around 3 or 4 (simple) object orientated programs so far and I've never had this issue. If it helps I'm using Xcode, but I get the same error in eclipse.

我用谷歌搜索无济于事,我还不太明白如何阅读 C++ 的正确文档,所以我有点卡住了。到目前为止,我已经编写了大约 3 或 4 个(简单的)面向对象的程序,但我从未遇到过这个问题。如果它有助于我使用 Xcode,但我在 eclipse 中遇到相同的错误。

It appears any method, regardless of return type, name, parameters defined in my head file give this error - however the constructor is fine. If PrintWords() is remove the project builds just fine.

似乎任何方法,无论返回类型、名称、参数在我的头文件中定义如何都会出现此错误 - 但是构造函数很好。如果 PrintWords() 被删除,项目构建就好了。

Any pointers will be very much appreciated!

任何指针将不胜感激!

回答by stellarossa

you declared it as voidbut you forgot to put it in the definition. should be:

您将其声明为,void但您忘记将其放入定义中。应该:

void StringList::PrintWords()

void StringList::PrintWords()

回答by 0x499602D2

Your member function PrintWordsis prototyped as:

您的成员函数PrintWords原型为:

void PrintOn();

Meaning it returns void. When you implement your function elsewhere you still have to provide the return type, which you've mistakenly left out:

意思是它返回void。当你在别处实现你的函数时,你仍然需要提供返回类型,你错误地忽略了它:

/* void */ StringList::PrintOn() { ... }

回答by Scott Mermelstein

Put a voidin front of the line giving you issues.

void在给你问题的行前面放一个。

Even though it feels redundant, you have to specify the return type both in the declaration and the implementation.

即使感觉多余,您也必须在声明和实现中指定返回类型。