Thread: Basic compile problem

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    20

    Basic compile problem

    Hi All

    I have the most simple situation, but I cannot compile it

    I have the following files: main.C, Book.C and Book.h:

    Book.h:
    Code:
    class Boek {
      public:
            Boek() ;
            ~Boek() {}
            void print() ;
    } ;
    Book.C:
    Code:
    #include "Book.h"
    
    Book::Book() {}
    
    void Book::print() {}
    main.C:
    Code:
    #include "Book.h"
    #include <string>
    #include <iostream>
    
    using namespace std ;
    
    int main() {
         Book book() ;
         book.print() ;
         return 0 ;
    }
    If I compile this code like: c++ main.C Book.C
    I get the following error:
    Code:
    main.C:9 error: request for member ‘print’ in ‘book’, which is of non-class type ‘Book ()()’
    If I remove the line book.print() eveything compiles fine
    Any suggestions what I do wrong here ?

    Thanks a lot
    LuCa

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Change "Book book();" to "Book book;".

    Edit: In addition to the obvious change of spelling "Book" correctly in Book.h.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If this is C++ code, why do your files end with a .c extention? It should be .cpp
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    20
    I thought for c++ (linux) the convention was capital .C
    So thats not true ?

    LuCa

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Could be, I use windows not linux so you could be right there. As long as the code works it is not a big problem
    Double Helix STL

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The man page for g++ says that it recognizes any of the following as C++ files:

    file.cc
    file.cp
    file.cxx
    file.cpp
    file.CPP
    file.c++
    file.C

    Personally I'd prefer .C since it's short, or .c++ since it's clear, but since most people seem to use .cpp I go along with that.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    20
    hmm, I've still problems.
    Now I've add a initialization method to Book.h[code]Book(std::string t): title(t) {}[code]
    However if I add the following line to main.C
    Code:
    Book book("The Code book") ;
    I get the following error
    Code:
    main.o: In function `main':
    main.C:(.text+0x1b5): undefined reference to `Book::Book(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
    main.C:(.text+0x1d0): undefined reference to `Book::~Book()'
    collect2: ld returned 1 exit status
    make: *** [xxxxx] Error 1
    Any suggestions what might be the reason for this to happen ?

    Thnx a lot
    LuCa

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > class Boek
    Maybe you still can't spell Book properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    20
    Book is just a work I use for here

    The strange thing is that I only get this error when I try to create a BOOK object: Book book("bla bla")
    So if I do nothing with the Book class it compiles fine...

    LuCa

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by luca
    Book is just a work I use for here

    The strange thing is that I only get this error when I try to create a BOOK object: Book book("bla bla")
    So if I do nothing with the Book class it compiles fine...

    LuCa
    You do not seem to have constructor that accepts const char* or const std::string as a parameter
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    20
    why 'const' ?

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    This is a linker error, which is odd since it implies that you got something right with your header.

    Please post your latest Book.h, Book.C, main.C
    Callou collei we'll code the way
    Of prime numbers and pings!

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    because "bla bla" is const char*
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by vart
    because "bla bla" is const char*
    You can create a non-const std::string from a char const *. The std::string parameter is weak enough for this function.
    Callou collei we'll code the way
    Of prime numbers and pings!

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by QuestionC
    You can create a non-const std::string from a char const *. The std::string parameter is weak enough for this function.
    It is absolutely opposite
    you can pass non-const value to the param that is declared as const.
    you cannot pass the const string to the param that is not const
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stream function compile problem
    By Know_Your_Role in forum C++ Programming
    Replies: 3
    Last Post: 06-02-2009, 12:03 PM
  2. problem trying to compile beej sample program
    By happyclown in forum Networking/Device Communication
    Replies: 7
    Last Post: 02-28-2009, 03:40 PM
  3. C++ compile problem
    By Rhuantavan in forum C++ Programming
    Replies: 3
    Last Post: 06-15-2002, 03:29 PM
  4. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 06:55 AM