1

I am trying to learn C++. Presently I am following a book called “Object Oriented Programing in C++” fourth edition (by Robert Lafore). In this book on page number 225 there is an example about object and classes. I have share the example below, and have manage to download included files and extracted them in to the project folder. So now my project file is contains a file called “msoftcon.c ” and “msoftcon.h”. When I try to compile the project I get an error “undefined reference to ‘init_graphics()’ ”, whereas this function is very much in msoftcon.c. Example is as follow:

 #include <iostream>
 #include "msoftcon.h"
 using namespace std;

 class circle
 {
        protected:

            int xCo, Yco;       //coordinates of center
            int radius;
            color fillcolor;    //color
            fstyle fillstyle;   //fill pattern

        public:

            void set(int x, int y, int r, color fc, fstyle fs)
                    {
                        xCo = x;
                        Yco = y;
                        radius = r;
                        fillcolor = fc;
                        fillstyle = fs;
                    }
            void draw()
                    {
                            set_color(fillcolor);           //set color
                            set_fill_style(fillstyle);      //set till
                            draw_circle(xCo, Yco, radius);  //draw solid circle
                    }

   };

 int main()
{
      init_graphics();

     return 0;
 }
dandan78
  • 13,328
  • 13
  • 64
  • 78
Study2C
  • 11
  • 4
  • what IDE are you using? – dandan78 Mar 19 '14 at 11:31
  • Do you *link* with the `msoftcon.c` file? If you rebuild your project files, does it list that source file (or its generated object file) in the build log? – Some programmer dude Mar 19 '14 at 11:33
  • "undefined reference to ‘init_graphics()" - means you have to add the required *library* that contains the *init_graphics()* function. And, please, always provide the COMPLETE error message! – SChepurin Mar 19 '14 at 11:37
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Benjamin Bannier Mar 19 '14 at 11:40

1 Answers1

2

Most likely you failed to add msoftcon.c and the associated .h to your project. Merely unpacking it in your project directly is not enough. It needs to be actually added to the project, after which it will be compiled and linked with the other files, hopefully eliminating the error you are seeing.

Exactly how that is done depends on your setup (IDE etc.).

dandan78
  • 13,328
  • 13
  • 64
  • 78