问题描述
我对C ++比较陌生(所以尝试并保持答案简单请!),我不明白为什么我得到错误: C ++需要一个类型说明符对于所有的声明,同时定义方法。
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.
我想写一个简单的程序读取文本文件逐行,将值存储到数组中。然而,我遇到一个问题,当试图声明我的.cpp文件中的方法。请在下面找到代码。
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.
StringList.h
StringList.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
#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
}
我已经google了无法使用,我不太明白如何读取适当的文档的C + +,所以我有点卡住。我写了大约3或4(简单)面向对象的程序,迄今为止,我从来没有这个问题。如果它帮助我使用Xcode,但我得到相同的错误在eclipse。
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.
它出现任何方法,无论返回类型,名称,参数定义在我的头文件给这个错误 - 但是构造函数是好的。如果PrintWords()是删除项目构建只是很好。
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.
任何指针都将非常感谢!
Any pointers will be very much appreciated!
推荐答案
它作为 void
,但是你忘了把它放在定义中。应为:
you declared it as void
but you forgot to put it in the definition. should be:
void StringList :: PrintWords()
这篇关于错误“C ++需要所有声明的类型说明符,同时定义方法”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!