0

I have taken this example directly from a book on C++ (shortened it so it's easier to see what the problem is). My class won't compile with g++. The class is:

   class stack{
       private:
           int count;
       public:
           void init(void);
   };

   inline void stack::init(void){
       count=  0;
  }

~
As you can see, I'm trying to prototype my functions inside the class, then define them outside the class. The book did exactly what I am trying this, but it doesn't work. Where is the mistake? Is it my computer (I'm using a mac). The error I get is the question, but also here:

user-MacBook-Pro:cplusplus trevortruog$ g++ Stack2.cpp
Undefined symbols for architecture x86_64:   "_main", referenced from:
      start in crt1.10.6.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
user1930111
  • 43
  • 2
  • 8

1 Answers1

1

The code compiles fine. It just doesn’t do anything useful since it’s missing a main function and therefore no executable can be generated from it.

This is not an error in the compiler but rather in the linker, which precisely complains about the lack of an entry point. You can see this from the error message:

ld: symbol(s) not found for architecture

The first thing, ld, is the name of the application which created the error message. ld is the linker application which is (internally) called by the actual compiler. Once that gets called, the code is already compiled.

Add a main function to solve the linker error.

As an added comment, the code uses bad practice. This is a sure hint that the programming book you’re using is bad. Unfortunately, bad teaching material is the bane of C++, which is a highly complex language even when taught correctly. Do yourself a favour and ditch the book in favour of a good one.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214