Expected primary expression?

This is a discussion on Expected primary expression? within the C++ Programming forums, part of the General Programming Boards category; Hey, I have a question - I am getting this compiler error: Code: 20 main.cpp expected primary-expression before "void" When ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Expected primary expression?

    Hey, I have a question - I am getting this compiler error:

    Code:
    20 main.cpp expected primary-expression before "void"
    When I call:

    Code:
    best tree;
    tree = new bst(void);
    bst.h
    Code:
    [clip]class bst
    {
    
    public:	
    	//------------------------------------------------------------
    	// Default constructor, creates an empty BST
    	//------------------------------------------------------------
    	bst(void);
    [clip]
    bst.cpp
    Code:
    bst::bst(void)
    {
        NodePool myNodePool;
    }
    I tried calling the function like:

    Code:
    bst tree;
    tree = new bst();
    but that gave me this error:

    Code:
    20 main.cpp no match for 'operator=' in 'tree = (((bst*)operator new(1u)), (<anonymous>->bst::bst(), <anonymous>))'
    Any help?
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    new bst() will return a bst*, so tree should be declared as a bst*.

    Of course, you don't necessarily have to use new, you might also be able to just create it locally:
    Code:
    bst tree;

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    When I don't use new the compiler gives me 'myNodePool undeclared' errors.

    When I make it *bst tree, the compiler gives me:

    Code:
    18 main.cpp expected primary-expression before "tree" 
    18 main.cpp expected `;' before "tree" 
    20 main.cpp `tree' undeclared (first use this function)
    And when I do bst *tree:

    Code:
    20 main.cpp expected primary-expression before "void" 
    52 main.cpp `modifyWord' has not been declared 
    52 main.cpp request for member of non-aggregate type before '(' token
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You're mixing and matching styles. Pick what you want and update all the code to match it, don't just try random things.

    If you want a regular object, use bst tree;. If you get an error about myNodePool undeclared, then fix that.

    If you want a pointer and dynamic memory allocation, then use bst* tree = new bst(); and when you call a member function of bst use tree->foo() instead of tree.foo() because that's how you use a pointer.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Ok, i'm just using a regular object.

    Code:
    bst tree;
    Now the error is:

    Code:
    21 bst.cpp `myNodePool' undeclared (first use this function)
    Here is my code excerpt:

    Code:
    using namespace std;
    
    bst::bst(void)
    {
        NodePool myNodePool;
    }
    
    bool bst::empty(void)
    {
        if(myNodePool[0].used == false) // line 21
            return false;
        else
            return true;
    }
    "bst tree" should call the standard constructor, right? Which would then create a new NodePool object myNodePool, so I don't understand why it is saying that myNodePool is undeclared. The default constructor for NodePool is:

    Code:
    NodePool::NodePool(void)
    {
        free = 0;
        
        for(int ii = 0; ii < SIZE-1; ii++)
        {
            pool[ii].left = ii+1;
            pool[ii].right = ii-1;
            pool[ii].used = false;
        }
    
        pool[SIZE-1].left = -1;
        pool[0].right = -1;
    }
    My file structure looks like: main.cpp, bst.h, bst.cpp, node.h, node.cpp. Each .cpp file includes the two .h files.
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> NodePool myNodePool;
    This line creates a local variable. That variable goes out of scope when that function ends.

    You probably want a member variable of the class, so add it inside the class definition (in bst.h), not inside the constructor.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Ok, added NodePool myNodePool in bst.h, however now it is telling me:

    Code:
    58 bst.h `NodePool' does not name a type
    So inside of bst.h I added #include "node.h", and now it tells me:

    Code:
    21 bst.cpp no match for 'operator[]' in '((bst*)this)->bst::myNodePool[0]'
    at the exact same place as above, at

    Code:
    if(myNodePool[0].used == false)
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Does NodePool have an operator[]?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Look, you can't just dump your chain of errors on us and expect us to divine what their cause is. You have the code, therefore you are best suited to fix the problems. Just give it a proper try.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Ok, here is my thinking. I need myNodePool to be accessible everywhere, just like my bst tree. But i want myNodePool to "inside" of bst, i.e. if I have a var "bst tree" then to access myNodePool it would look something like this: tree.myNodePool[val].var. So I added "NodePool myNodePool" under the public definitions in bst.h - so do I have to do anything else with myNodePool inside of bst.cpp? Just by declaring it in bst.h should it call the default constructor for NodePool, correct?

    I've added this code to node.cpp:

    Code:
    Node NodePool::operator[] (int indx)
    	{
    	char errStr[99];
    	if (indx >= SIZE || indx < 0)
    		{
    		sprintf(errStr, "&#37;s %d: Index %d not in range 0 to %d", __FILE__, __LINE__, indx, SIZE);
    		throw range_error(errStr);
    		}
    	if (!pool[indx].used)
    		{
    		sprintf(errStr, "%s %d: Index %d refers to a node on the freelist.", __FILE__, __LINE__, indx);
    		throw range_error(errStr);
    		}
    	return pool[indx];
    	}
    And: (node.h)

    Code:
    class NodePool
    {
      ...
      public:
        NodePool(void);
        Node operator[](int indx);
    ...
    I think that I am getting myself really confused. When I asked my professor "Now would the default bst constructor be the one to create the new NodePool object?" he replied "Yes". However that just creates a local variable.

    Now when I compile I am only getting this error:

    Code:
    37 bst.cpp expected primary-expression before "void"
    Code excerpt:

    Code:
    int bst::insert (string word, int lineno)
    {
        // Check to see if the string is already in the BST
        int duplicate;
        duplicate = search(word);
        if(duplicate == -1)
        {
            // No duplicate found, get next available node
            next = myNodePool.get(void); // line 37
            myNodePool[next].value = word;
            myNodePool[next].lineNumber = lineno;
            myNodePool[next].used = true;
            return 0;
        }
        else
            return -1;
    }
    Last edited by Beowolf; 11-08-2007 at 06:02 PM.
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> Just by declaring it in bst.h should it call the default constructor for NodePool, correct?
    Yes. The constructor for bst will automatically default construct your NodePool. That code is correct.

    In fact, your solution for the error on line 54 seems correct as well, did you try it?

    Depending on what NodePool is trying to do you might want it to be a Node& return value instead of just a Node, in which case you should make sure to update the declaration and the definition of the operator[].

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    My solution for line 54 worked, yes.

    I changed code to:

    Code:
    Node& NodePool::operator[] (int indx)
    {
      ...
    }
    And updated the declaration to:

    Code:
    Node& operator[](int indx);
    But I am still getting this error:

    Code:
     bst.cpp In member function `int bst::insert(std::string, int)': 
    37 bst.cpp expected primary-expression before "void"
    At (bolded)

    Code:
    int bst::insert (string word, int lineno)
    {
        // Check to see if the string is already in the BST
        int duplicate;
        duplicate = search(word);
        if(duplicate == -1)
        {
            // No duplicate found, get next available node
            next = myNodePool.get(void);
            myNodePool[next].value = word;
            myNodePool[next].lineNumber = lineno;
            myNodePool[next].used = true;
            return 0;
        }
        else
            return -1;
    }
    If I might ask, what is the benefit of making it an address Node& instead of just Node?
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You NEVER specify "void" in the call to a function. It is used in the declaration of a function to specify that there are no parameters to the function. If you have a call to such a function, just leave the parenthesis empty, and it will work just fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> what is the benefit of making it an address Node& instead of just Node?
    You are making it a reference, not an address. The & symbol is used for two different things in C++. When applied to a variable it means get the address. When applied to a type (like here) it means make the type a reference.

    By making the return value a reference, it means that any changes you make to the return value will be reflected in the NodePool. If you don't make it a reference, it means a copy of the value will be returned so any changes you make will not affect the original data.

    I didn't want you to make that change without understanding what it does. Returning a Node and returning a Node& are two different things, and I don't know which one you really want so you should decide which to use based on whether you want the data in the NodePool to be updated when you change the value returned by the operator[].

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    (void) is an old C throwback, in C++ always just use ().
    It makes no sense to try passing a type as a function parameter either, so in that last case it was horribly wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected primary expression before "." token
    By melodious in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 04:39 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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