This time I am making an orderedLinkedList program, which will not compile for me. The code is all way too long to put on here, so I will show you the errors I am getting. Please give me any input on what you think the errors could be caused by. I cannot find any help searching on google. There are 3 files for this program...2 .h files and 1 .cpp file. The code is straight from the book as it shows it, and I still get these errors. I do not know why I am getting a "not declared in scope" error. I went back and checked through the book and the code is matched perfectly for all 3 files.

In file included from testProgLinkedList.cpp:2:
orderedLinkedList.h: In member function `bool orderedLinkedListType<Type>::search(const Type&)':
orderedLinkedList.h:43: error: `first' was not declared in this scope
orderedLinkedList.h: In member function `void orderedLinkedListType<Type>::insertNode(const Type&)':
orderedLinkedList.h:74: error: `first' was not declared in this scope
orderedLinkedList.h:77: error: no post-increment operator for type
orderedLinkedList.h:97: error: no post-increment operator for type
orderedLinkedList.h:103: error: no post-increment operator for type
orderedLinkedList.h: In member function `void orderedLinkedListType<Type>::deleteNode(const Type&)':
orderedLinkedList.h:116: error: `first' was not declared in this scope
orderedLinkedList.h:150: error: no post-decrement operator for type

Recommended Answers

All 7 Replies

It looks like you are playing with template classes. You might want to read up on templates over at http://www.parashift.com/c++-faq-lite/

The first problem is that there is a variable or class or function named first being used somewhere in the code (lines 43, 74, 116) that either doesn't exist or exists in another namespace.

The second problem is that with templates, you don't know what the type is, so you use a substitute. For example

template <T> T succ( T a ) {
  return ++a;
  }

int i = 42;
std::cout << succ( i );  // works great: prints 43 and i still == 42

std::string s = "Hello, world!";
std::cout << succ( s );  // fails miserably

Somewhere in the code a variable of type Type is being post-decremented ( x-- ). If x is, say, a string then the template fails, since decrementing a string is not defined (it makes no sense).

I have been reading on other forums of people having the same problem, but they were able to fix it by compiling the program with VC ++ 6. I have to make it compile in SSH secure shell, and it doesn't appear to be happening for me.

Without seeing your code I can't really give further help.

SSH just lets you connect to another computer. It shouldn't affect your compiler. That said, though, you might not be using the same compiler on both ends. For example, say you are at home using Windows and make a program with VC++. Then you use SSH to connect to the school computer running Linux. If you copy the program source files over there and try to compile on the school's computer you will not be using VC++ but likely GCC or some other OS-specific CC. If your code isn't strictly ISO compliant you will have problems.

I presume you know this and that I am only stating the blaringly obvious but easily overlooked.

Try using std::

Here is the code for insertNode, which is causing some of errors above. Can I use std:: for this?

template<class Type>
void orderedLinkedListType<Type>::insertNode(const Type& newItem)
{
        nodeType<Type> *current; //pointer to traverse the list
        nodeType<Type> *trailCurrent; //pointer just before current
        nodeType<Type> *newNode;  //pointer to create a node

        bool  found;

        newNode = new nodeType<Type>; //create the node
        assert(newNode != NULL);

        newNode->info = newItem;   //store newitem in the node
        newNode->link = NULL;      //set the link field of the node
                                   //to NULL

        if(first == NULL)  //Case 1
        {
           first = newNode;
           count++;
        }
        else
        {
           current = first;
           found = false;

           while(current != NULL && !found) //search the list
                        if(current->info >= newItem)
                                found = true;
                else
                        {
                                trailCurrent = current;
                                current = current->link;
                        }

           if(current == first)         //Case 2
           {
                        newNode->link = first;
                        first = newNode;
                        count++;
           }
           else                         //Case 3
           {
                        trailCurrent->link = newNode;
                        newNode->link = current;
                        count++;
           }
        }//end else
}//end insertNode

Building your first hand rolled linked list is hard enough, let alone trying to do it with templates.

As to your post #6 in this thread, there is nothing there that needs something out of namespace std. If you have a statement such as:

using namespace std;

after you list of included header files and before declaring int main(), you won't have to usd the std:: syntax with each item you use from within namespace std.

In post #6 of this thread it looks like you need a { betwee lines 27 and 28 and a } between lines 35 and 36.

Make sure you go read the stuff at the C++ FAQ lite that I linked for you.

Use the word typename instead of class in template declarations. Yes, I know... semantics...
Somewhere you should have something that looks like this:

template<typename Type>
class orderedLinkedListType {
  ...
  nodeType<Type> first;
  int count;
  ...
  public:
    void insertNode( const Type & );
  ...
  }

Notice that both first and count are declared in the class. Make sure that they are in your code also. (I didn't know exactly where to put them. It is OK if they are after the public keyword.)

Also make sure that count is an integer (it is the thing being post-incremented: count++; ).

On line 28 the nodeType items are being compared with the >= operator. Make sure your nodeType template class overloads that operator properly.

You don't need to insert the { and } that Lerner suggested, but I would recommend being more careful with your indentation. (The else is missing a tab indent.)

Let me know if this helps. Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.