Bill
Bill

Reputation: 145

I am creating a DLL in C++ but it is giving me a persistant error

I am creating a DLL using the code provided to us by my instructor. However I have tried to compile it at home and it does not seem to work. Any help would be appreciated.

template<class T>
class mySTLlist : public list<T> {
public:
    void addInMiddle(T t){}

    friend ostream& operator<<(ostream& out,  mySTLlist<T>& lst) {
        for(mySTLlist<T>::iterator i = lst.begin(); i != lst.end(); i++)
            out << *i << ' ';
        out << '\n';
        return out;
};

It gives me an Error at:

mySTLlist<T>::iterator i = lst.begin(); 

It says that i need a ; before it and it is not declared. I am relatively new to C++

Upvotes: 0

Views: 74

Answers (2)

Brooks Moses
Brooks Moses

Reputation: 9527

This is a good illustration of why it is important to include a complete example, and also to read all of the error messages. Your code is missing some include headers; at minimum, it needs the following at the top:

#include<list>
#include<iostream>
using namespace std;

When I correct those and add the missing } at the end, and compile it, I get three errors:

foo.cpp:14:9: error: need 'typename' before 'mySTLlist<T>::iterator' because
'mySTLlist<T>' is a dependent scope
foo.cpp:14:32: error: expected ';' before 'i'
foo.cpp:14:49: error: 'i' was not declared in this scope

The first one says that we need to add "typename" (note that this is in quotes, meaning the literal keyword typename, not the name of a type), so we add exactly what it says we need, changing that line to:

for(typename mySTLlist<T>::iterator i = lst.begin(); i != lst.end(); i++)

That fixes the problem. The error that you are seeing is a follow-on error -- because the declaration of i was buggy, it skipped over it to see what it could do with the rest of the file. The next time you use i, it complains that it hasn't been declared (which is, of course, because it skipped the declaration) -- and, likewise, the missing ; error is because of how it's skipping past that first error. So, fix the first problem, and that fixes the rest.

Upvotes: 2

vitakot
vitakot

Reputation: 3844

You have to add an iterator typedef:

typedef typename mySTLlist<T>::iterator myListIter;

and then write:

friend ostream& operator<<(ostream& out,  mySTLlist<T>& lst) {
    for(myListIter i = lst.begin(); i != lst.end(); i++)
        out << *i << ' ';
    out << '\n';
    return out;

Upvotes: 1

Related Questions