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

Lorienas picture Lorienas · Nov 5, 2013 · Viewed 81k times · Source

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.

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

#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
}

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.

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!

Answer

stellarossa picture stellarossa · Nov 5, 2013

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

void StringList::PrintWords()