Newbie question, error "expected primary-expression before objectname"

This is a discussion on Newbie question, error "expected primary-expression before objectname" within the C++ Programming forums, part of the General Programming Boards category; Hi, I am a complete newbie to C++, I come from Perl and used to know a little C++, but ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    Newbie question, error "expected primary-expression before objectname"

    Hi,

    I am a complete newbie to C++, I come from Perl and used to know a little C++, but I very newbie. I am just trying to include a header file and its class (I get no errors from the include) and then create an object. Thats all that I'm doing at this point, except for some stuff that also worked without error, taking command line variables and printing them back out (that stuff I won't copy here, I promise you it works). The bug is either in my include and call to the constructor in the main file (hello_world.c) or its in the header file or the class file or something to with with how I include them. The bug from g++ is "expected primary-expression before publicagent" and "expected ";" before publicagent"

    My code goes like this:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include "PublicFirm.h"
    
    // global variables and user input
    int input;
    ... // 5 more
    
    int main (int argc, char* argv[])
    {
    
      if( argc != 6 )
        {
        cout << "Usage ...\n";
        exit(0);
        }
    
    cout<< "hello\n";
    
     input = atoi(argv[1]);
     ... // 5 more
    
    cout<< "Okay, strarting...";
    
    PublicFirm publicagent;
    
    cout << "instantiated\n";
    
    return 0;
    
    }
    
    
    The header and class files look like this:
    
    header:
    
    #ifndef __PublicFirm__
    #define __PublicFirm__
    
    namespace PublicFirm
    {
    
    class PublicFirm
      {
    // trial
      private:
        int privatevalue; 
    
    // constructor
    // destructor
    // trial method
    
      public:
        PublicFirm(); 
        virtual ~PublicFirm(); 
        virtual int getValue(); 
    
      };
    
    
    }
    
    #endif
    
    
    the class file:
    
    #include "PublicFirm.h"
    #include <stdio.h>
    
    
    namespace PublicFirm
    {
    
      PublicFirm::PublicFirm()
      {
        //cout << "Constructing...";
        //privatevalue += 1;
    
      }
    
      PublicFirm::~PublicFirm()
      {
        //cout << "Destructing...";
    
      }
    
      int PublicFirm::getValue(int i)
      {
        //cout << privatevalue;
        //return privatevalue;
      }
    
    }

    -- I commented most of it out for debugging...

    So, am I missing something such that the files are not included properly? Or is there some other obvious bug in there?

    any thoughts appreciated!
    Last edited by Ken Fitlike; 09-06-2006 at 12:45 PM. Reason: code tags added

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Use CODE tags, please. You usually don't get asked nicely, twice.

    You're defining your class under a namespace, yet you never appear to qualify the identifier with that namespace when you instantiate an object from it
    Code:
    PublicFirm::PublicFirm publicagent;
    On that note, you should never use the same identifier for a class and a namespace. Consider renaming the namespace to PbFi or something.
    Last edited by SlyMaelstrom; 09-06-2006 at 12:48 PM.
    Sent from my iPadŽ

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Please use code tags when you post code - it makes it much easier to read!

    Code:
    ... // 5 more
    You have three dots here that come before a comment, the compiler is reading the dots and
    thinking they are linked to somthing - this is one error i did see, but i cant read the code when it is not tagged, it is hard to follow

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    sorry

    new here, sorry about the code tags. Also, this post posted twice since I thought it had timed out.

    No, those dots were not in the actual code, that was me saying "..." eg you don't need to read this print statement.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    Ah, thank you!

    Quote Originally Posted by SlyMaelstrom
    Use CODE tags, please. You usually don't get asked nicely, twice.

    You're defining your class under a namespace, yet you never appear to qualify the identifier with that namespace when you instantiate an object from it
    Code:
    PublicFirm::PublicFirm publicagent;
    On that note, you should never use the same identifier for a class and a namespace. Consider renaming the namespace to PbFi or something.
    Advice taken. That does seem to be at least part of the problem (that I did not use the namespace qualifier). The error message has changed at least

    I tried Public as the namespace, then realized that might be reserved, so I am using PublicF. After changing it in all the files, I get a new error.

    Now, g++ says "undefined reference to PublicF::PublicFirm::PublicFirm()"

    At least I am making progress

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by newbiegl
    Now, g++ says "undefined reference to PublicF::PublicFirm::PublicFirm()"
    So, I'd say either you aren't including the implementation file in the project (highly unlikely), or you forgot to change the identifier of the namespace wrapping your implementation to PublicF(most likely).
    Sent from my iPadŽ

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    It means you have not delcared PublicFirm(), is this a linker error or a compiler error?
    If it is linker error make sure you have delcarted the function in the header file, so the linker can find it.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by SlyMaelstrom
    So, I'd say either you aren't including the implementation file in the project (highly unlikely), or you forgot to change the identifier of the namespace wrapping your implementation to PublicF(most likely).
    I have the inlude at the top of the hello_world file. I changed the namespace everywhere and I just tried it without using explicit namespaces (just using the class) -- how important is it to have a separate namespace anyway? If a namespace is only going to have one class, can I just use the class as the namespace?

    Anyway, without using explicit namespace, I get the error "undefined reference to PublicFirm::PublicFirm()" ... for constructor, destructor and the other method. Why is it undefined? hmm... I know I can figure this out

    Thanks for being so helpful guys!

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by newbiegl
    I have the inlude at the top of the hello_world file. I changed the namespace everywhere and I just tried it without using explicit namespaces (just using the class) -- how important is it to have a separate namespace anyway? If a namespace is only going to have one class, can I just use the class as the namespace?
    It's not that important to have a namespace in such a small project.

    Quote Originally Posted by newbiegl
    Anyway, without using explicit namespace, I get the error "undefined reference to PublicFirm::PublicFirm()" ... for constructor, destructor and the other method. Why is it undefined? hmm... I know I can figure this out
    Assuming your constructor is public, as you show in your post, then I see no reason for undefined references. My eyes could be decieving me, but it looks fine from here.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    linker error

    Quote Originally Posted by swgh
    It means you have not delcared PublicFirm(), is this a linker error or a compiler error?
    If it is linker error make sure you have delcarted the function in the header file, so the linker can find it.
    Its a linker error, but I have declared it in the header file. I tried PublicFirm() and PublicFirm::PublicFirm()

    Gosh, I know this looks so pathetic. Yes, I need to read some books; have one but its a bit advanced and am waiting for another in the mail :-/

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by SlyMaelstrom
    It's not that important to have a namespace in such a small project.
    Is it good practice for a larger project? Ultimately this (once I know enough C++ to do so) will be a maybe 5-10 class program, maybe 1500-2500 lines of code. Or maybe more lines than that, I'm used to Perl

    Why have a namespace around a class if the class already provides a namespace? If there is only one class in the namespcae, why declare both a namespace and a class name? What am I missing?

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Go for Teach yourself C++ programming in 24hrs, or grab C++, how to program the 4th edition, two of the best C++ books on the market

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    books

    Quote Originally Posted by swgh
    Go for Teach yourself C++ programming in 24hrs, or grab C++, how to program the 4th edition, two of the best C++ books on the market
    I have C++ Primer Plus coming in the mail. I think I almost bought the "how to program" book, but the primer plus had better reviews.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You use namespaces to group together classes, free functions and other identifiers that form a functionality package. The granularity is up to you and depends greatly on the size of the project and the diversity of purpose it has.

    As for the error, it is a linker error. gcc is telling you that although it knows the constructor is there, it can't find the implementation anywhere and thus can't create the complete executable. How do you compile your project?
    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

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    It all depends how much C++ you are willing to learn and how much you really care.
    If you want to use C++ s a hobbie, use a good book like primer plus or how to program C++ and program till your eyes drop out, then in a few months years, you can make some outstanding programs. Of course, taking it up as a proffesion would mean even more learning and more commitment. As you have learned Peal, you should be used to some of the syntax of the language all ready which is a good start. If you want a really paced book with tons of great information, get Acelleratred C++ book (its red!).
    You seem to keen to learn, and that is good to know.

    Your first program looks a little advanced. but you said you had some C++ experience so I can see why you chose a challenging program to begin with, getting it to work is only half the battle. Wanting to learn and ask questions is the other half.

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

Similar Threads

  1. Newbie question
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-13-2009, 11:10 PM
  2. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  3. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  4. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  5. Primary colors question
    By Robin Hood in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2002, 04:30 AM

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