Thread: Class/linking question - getting odd error

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    Class/linking question - getting odd error

    I'm trying to build a stack class. Bascailly it consists of the following files:

    Code:
    // listnode.h
    #ifndef LISTNODE_H
    #define LISTNODE_H
    
    class ListNode {
    public:    
        int data; // stores the integer
        ListNode *next; // pointer to next ListNode
    };
    
    
    #endif
    Code:
    // stack.h
    #ifndef STACK_H
    #define STACK_H
    
    #include "listnode.h"
    
    class Stack {
    
    public:
        Stack(); // constructor
        ~Stack(); // destructor
        int pop(); // remove top item in stack
        void push(int); // add item to stack
        int top(); // return item from top of stack
        void print(); // print contents of stack
        bool isEmpty(); 
        
    private:
        ListNode *topNode; // top listnode in list
    };
    
    #endif
    Code:
    // stack.cpp
    #include <iostream>
    using std::cout;
    
    #include "stack.h"
    
    // constructor
    Stack::Stack() {
        topNode = NULL;
    }
    
    // destructor
    Stack::~Stack() {
        delete topNode;
    }
    
    void Stack::push(int item) {
        ListNode *node = new ListNode();
        node->data = item;
        node->next = topNode;
        topNode = node;
    }
    
    int Stack::pop() {
        assert(topNode != NULL);
        int temp = topNode->data;
        topNode = topNode->next;
        return temp;
    }
    
    void Stack::print() {
        ListNode *node = topNode;
        
        while(node != NULL) {
            cout << node->data << ' ';
        }
        
        if(node == NULL)
            cout << "Stack is empty!";
    }
    
    .. etc
    When I compile a file called stacktest.cpp I get the following error:

    stacktest.cpp request for member `print' in `testStack', which is of non-class type `Stack ()()'
    That file basically looks as follows:

    Code:
    // stackTest.cpp
    #include <iostream>
    using std::cout;
    using std::cin;
    
    #include "stack.h"
    
    int main() {
        Stack testStack();
        testStack.print();
            
        cin.get();
        return 0;
    }
    Any ideas as to what I've done incorrectly?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just on a quick guess, what if you change:
    Stack testStack();
    to
    Stack testStack;

    I am never really sure about these things without testing, but it looks like you are running into the function declaration syntax problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    I think I was incorrect in using Stack testStack(). Anyhow, I changed it to Stack testStack, but now I get the following:

    stacktest.cpp: undefined reference to `Stack:rint()'

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you sure the stack sourcefile was compiled and linked?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Yes, it was a linking problem, but it's sorted out now.

    Thanks for your assistance laserlight. Stay cool man.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    err... she's a lady actually
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM