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; Originally Posted by CornedBee You use namespaces to group together classes, free functions and other identifiers that form a functionality ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by CornedBee
    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.
    Okay, so within each class file you would not need a namespace if only a class was in there; but if you wanted to include a class and some other things, you could use the namespace to design the architecture more cleanly, including those all under one namespace...?

    Quote Originally Posted by CornedBee
    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?
    I am just calling 'g++ -o hello_world hello_world.c' from a cygwin shell

  2. #17
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> 'g++ -o hello_world hello_world.c'

    should that not be .cpp?

  3. #18
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Quote Originally Posted by newbiegl
    I am just calling 'g++ -o hello_world hello_world.c' from a cygwin shell
    What about the implementation for the class are you compiling and linking that?
    Sent from my iPadŽ

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It should. That aside, it won't work. The compiler calls upon the linker, which can't find the definition of the class's member functions (they're in the other .cpp file), so it throws an error.
    You have a few options.
    1) Compile all files together:
    g++ -o hello_world hello_world.cpp PublicFirm.cpp
    This has the disadvantage of recompiling everything every time.
    2) Compile files separately, then link:
    g++ -c hello_world.cpp
    g++ -c PublicFirm.cpp
    g++ -o hello_world hello_world.o PublicFirm.o
    Advantage is that you only need to recompile the files that changed. Disadvantage is that you always have at least two commands after every change; if a header that many files depend on changes, you have many more.

    3) Use a build script. There are many. Makefiles are simple on this low level, but either you do dependency tracking manually or they're not simple anymore. Boost.Build is very powerful and easy to do once you understand it, but it's difficult to set up and learn.
    I recommend using SCons. It's easy and powerful, but you need Python installed.
    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

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by twomers
    >> 'g++ -o hello_world hello_world.c'

    should that not be .cpp?
    Yeah, but it doesn't matter, as I understand.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by CornedBee
    It should. That aside, it won't work. The compiler calls upon the linker, which can't find the definition of the class's member functions (they're in the other .cpp file), so it throws an error.
    You have a few options.
    1) Compile all files together:
    g++ -o hello_world hello_world.cpp PublicFirm.cpp
    This has the disadvantage of recompiling everything every time.
    2) Compile files separately, then link:
    g++ -c hello_world.cpp
    g++ -c PublicFirm.cpp
    g++ -o hello_world hello_world.o PublicFirm.o
    Advantage is that you only need to recompile the files that changed. Disadvantage is that you always have at least two commands after every change; if a header that many files depend on changes, you have many more.

    3) Use a build script. There are many. Makefiles are simple on this low level, but either you do dependency tracking manually or they're not simple anymore. Boost.Build is very powerful and easy to do once you understand it, but it's difficult to set up and learn.
    I recommend using SCons. It's easy and powerful, but you need Python installed.

    D'oh!

    Thanks

    Thats the thing about being a newbie. Oops. Thanks for the detailed reminder, I once knew all that

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by swgh
    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.

    Thanks for the vote of confidence. Yeah, I figured that I needed the primer plus book to get started, I also have Stroustrup coming in the mail (I once owned it, but don't know where its gone) and I have this more advanced OO book Thinking in C++, which I will wait on until I have the syntax and stuff down (I'm sure I can understand it, but won't be able to use it yet).
    ... actually, flipping through it I don't think it is very advanced. Maybe I could have found my answers there - wow, I'm gonna start reading it tonight. The introduction and reviews made me think that it was more all about the OO and not so much about the basics.

    Anyway, thanks so much. I think I can make it link now.

  8. #23
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Quote Originally Posted by newbiegl
    I also have Stroustrup coming in the mail
    Careful with him... I heard he eats, a lot.
    Sent from my iPadŽ

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Whoohoo!

    Got it linked and running...

  10. #25
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by newbiegl
    Okay, so within each class file you would not need a namespace if only a class was in there; but if you wanted to include a class and some other things, you could use the namespace to design the architecture more cleanly, including those all under one namespace...?
    Yeah, namespaces are useful for either a collection of classes and functions. For example, a linear algebra package (with matrix/vector classes, operators, and functions) might be linear:: or somehting like that.

    I have my own text:: which contains a bunch of functions all related to working with strings (e.g. functions to change the character set that a string is encoded in).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Page 2 of 2 FirstFirst 12
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