Class/linking question - getting odd error

This is a discussion on Class/linking question - getting odd error within the C++ Programming forums, part of the General Programming Boards category; I'm trying to build a stack class. Bascailly it consists of the following files: Code: // listnode.h #ifndef LISTNODE_H #define ...

  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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,794
    Are you sure the stack sourcefile was compiled and linked?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Portugal
    Posts
    7,488
    err... she's a lady actually
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 06: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, 12:10 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21