Descomposing your program in .hpp and in .cpp

Im having problems with this program.
I get this only error [Linker error] undefined reference to `Monomio::Monomio(int const&, unsigned int const&)' (that is Monomio's constructor) in Main.cpp. The problem is that i always have that error, no matter what the code is.

Im trying to descompose my program in parts, but i don't really know how what im doing wrong. I hope someone can lend me a hand

(by the way.. The code of the methods is correct, and everything is under the same folder )

Header and prototypes: Monomio.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**==============================================================================
                                         CLASE MONOMIO
================================================================================*/

# ifndef _MONOM_H
# define _MONOM_H

// --------------
// - Directivas -
// --------------

# include <iostream>
# include <string>
# include<sstream>

using namespace std ;

// ~~~~~~~~~~~~~~~~
// - Clase Monomio
// ~~~~~~~~~~~~~~~~

class Monomio {
   private:
      int coef_; //Coeficiente entero
      unsigned exp_;   // Exponente natural
   public:
      Monomio(const int &ce = 0,const unsigned &ex = 0);
      unsigned exp(void) const;
      int coef(void) const;
      void exp(const unsigned& a);
      void coef(const int& a);
      int evalue(const int &valor) const;
      string  toString(void) const;
      bool operator==(const Monomio& m) const ;           
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// - Sobrecarga de Operadores -
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//saca un monomio por el flujo de salida

ostream &operator << (ostream &stream, const Monomio &m);
# endif


Method bodies : Monomio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**==============================================================================
  CLASE MONOMIO : Cuerpos de funciones/metodos y sobrecargas
================================================================================*/

# ifndef _MONOM_C
# define _MONOM_C

// ~~~~~~~~~~~~~~
// - Directivas -
// ~~~~~~~~~~~~~~
# include <iostream>
# include <string>
# include <sstream>
# include "Monomio.hpp"

// ~~~~~~~~~~~~~~~~
// - Definiciones -
// ~~~~~~~~~~~~~~~~

using namespace std ;

// ~~~~~~~~~~~~~~~~~~~~~
// - Funciones Monomio -
// ~~~~~~~~~~~~~~~~~~~~~


//toString devuelve una cadena String que representa al monomio

string Monomio :: toString(void) const
{
   stringstream temp;
   temp << "(" << coef_<< ")*X^" << exp_;
   return temp.str();
};

//evalue : devuelve el valor del monomio para una x determinada

int Monomio::evalue(const int &valor) const 
{
   int temp = 1;
   for (unsigned i = 1; i <= exp_; i++){temp *= valor;};
   temp*=coef_;
   return temp; 
}

//Constructor

Monomio :: Monomio(const int &ce,const unsigned &ex)
{
   coef_ = ce;
   exp_ = ex;
}

//coef : devuelve el coeficiente

int Monomio::coef(void) const
{
   return coef_;
}

//exp : devuelve el exponente

unsigned Monomio :: exp(void) const 
{
   return exp_;
}

//coef : devuelve el coeficiente

void Monomio::coef(const int& a) 
{
   coef_ = a;
   return;
}

//exp : devuelve el exponente

void Monomio :: exp(const unsigned& a) 
{
   exp_= a;
   return;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// - Sobrecarga de Operadores -
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//saca un monomio por el flujo de salida

ostream &operator<< (ostream &stream, Monomio &m)
{
   stream << m.toString();
   return stream ;
} 

bool Monomio::operator==(const Monomio& m) const 
{
   return ((exp()== m.exp())&&(coef()== m.coef()));
}


# endif //_CLASS_C 


Main file : main.cpp
(here i get [Linker error] undefined reference to `Monomio::Monomio(int const&, unsigned int const&)' )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**=================================================================================**/

// ~~~~~~~~~~~~~~
// - Directivas -
// ~~~~~~~~~~~~~~
# include <iostream>
# include <string>
# include <sstream>
# include "Monomio.hpp"

// ~~~~~~~~~~~~~~~~
// - Definiciones -
// ~~~~~~~~~~~~~~~~
//# ifndef DEBUG
//# define DEBUG
//# endif

using namespace std;


// ~~~~~~~~~~~~~
// -    MAIN   -
// ~~~~~~~~~~~~~

   int main (void)
   {
      Monomio a(3,2);
      cout << "a";
      char manolo[2];
      cin >> manolo;
      return 0;
   }
Last edited on
Are you linking the two sourcefiles?
EDIT : (Bazzy) What do you mean by linking the two sourcefiles?. Do you mean, creating Monomio.o from Monomio.hpp and Monomio.cpp ?

Somehow i got it working, using Dev C++ 's projects. It autogenerates a makefile . I can use all of Monomio's methods. However, I can't use the operator << with monomios on main.cpp (and i had already overloaded that operator)

1
2
3
4
5
6
7

   int main (void)
   {
      Monomio a(3,2);
      cout << a;
      return 0;
   }


the error i have is
[Linker error] undefined reference to `operator<<(std::ostream&, Monomio const&)'

Any ideas?
Last edited on
How are you compiling these? Are you compiling from command line?

You should compile with something like gcc monomio.cpp main.cpp -Wall -pedantic -lstdc++ -o monomio.exe or
g++ monomio.cpp main.cpp -Wall -pedantic -o monomio.exe...
Last edited on
Im afraid im compiling using Dev C++ , so i don't use any command lines.
(this might sound ultra noob but i don't know how to get g++ on my windows console )
maybe its that what makes this not work.

Maybe i should use a custom makefile.. i don't know what to do
Last edited on
You have to make sure g++ (dev-c++ is an IDE) is compiling both files; if you only compile main.cpp then monomio.cpp doesn't exist.

You need to create monomio.o and main.o and then link them with ld. GCC/G++ should do that automagically.

(this might sound ultra noob but i don't know how to get g++ on my windows console )

You have to change your PATH variable; open regedit and go to hkey_local_machine\system\currentcontrolset\control\session manager\environment\ and open the REG_EXPAND or REG_SZ called "PATH". Add "location_of_g++" to the end. Obviously replace "location_of_g++" with the actual location, e.g. "C:\ProgramFiles\g++". That will configure command prompt to search in that folder for executables. Alternatively you could just put g++.exe and all of the other programs it needs (as.exe and ld.exe for example) in the system32 folder.

Take care not to do anything else to your path variable, such as delete things from it or cmd won't work properly (for example if you have nmap port scanner and you removed "%PROGRAMFILES%\nmap" then you wouldn't be able to use nmap from commmand line anymore). Moving g++ to system32 is probably the better idea. Make sure you copy gcc, g++, as, ld and any other dependant programs to the folder too, or gcc and g++ won't work. They will identify you when you try to run them if there are any broken dependancies though; for example, gcc might print "Installation error, cannot find ld" or something. I forget what the actual error message is.
No problem.
Whoops..i delete the answer.
Anyways here is the link i posted previously
http://www.cplusplus.com/forum/beginner/6007/

Thanks A LOT for the help. (also, for explaining me how to make g++ work on the console) Im marking this as solved
Topic archived. No new replies allowed.