Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 476
Lorienas Avatar asked Nov 05 '13 22:11

Lorienas


2 Answers

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

void StringList::PrintWords()

like image 87
stellarossa Avatar answered Oct 09 '22 09:10

stellarossa


Your member function PrintWords is prototyped as:

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 */ StringList::PrintOn() { ... }
like image 25
David G Avatar answered Oct 09 '22 10:10

David G