"undefined reference to" statement
Jan 2, 2013 at 11:12pm UTC
Hello, I'm doing exercises on classes in c++ and I'm writing a program that should store and convert Roman numbers in decimal numbers. I'm compiling with code::blocks
This is
RomanType.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#ifndef ROMANTYPE_H
#define ROMANTYPE_H
const int SIZE = 15;
class RomanType
{
public :
void getrom(char str[]); // store roman number
int getnum(char str[]); //convert in decimal form and store it
void print() const ;
RomanType();
RomanType(char str[]);
protected :
private :
char rom[SIZE];
int num;
};
#endif // ROMANTYPE_H
RomanType.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
#include "RomanType.h"
#include <cstring>
#include <iostream>
RomanType::RomanType()
{
for (int i=0; i<SIZE; i++)
rom[i] = '\0' ; //ctor
num = 0;
}
RomanType::RomanType(char str[])
{
strcpy(rom, str);
num = 0;
}
void RomanType::getrom(char str[])
{
strcpy(rom, str);
}
int RomanType::getnum(char str[])
{
for (int i=0; i<SIZE; i++)
{
switch (rom[i])
{
case 'M' :
num =+ 1000;
break ;
case 'D' :
num =+ 500;
break ;
case 'C' :
num =+ 100;
break ;
case 'L' :
num =+ 50;
break ;
case 'X' :
num =+ 10;
break ;
case 'V' :
num =+ 5;
break ;
case 'I' :
num =+ 1;
break ;
default :
cout << "Invalid input" ;
break ;
}
}
void RomanType::print() const
{
cout << num;
cout << rom;
}
}
and finally
main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <cstring>
#include "RomanType.h"
using namespace std;
int main()
{
char str[SIZE];
int dec;
RomanType myrom;
cout << "Enter a number in roman form:" ;
cin >> str;
myrom.getrom(str);
myrom.print();
return 0;
}
This is my debug window referring to main function
||=== numeri, Debug ===|
obj\Debug\main.o||In function `main':|
D:\programmic++\numeri\main.cpp|11|undefined reference to `RomanType::RomanType()'|
D:\programmic++\numeri\main.cpp|16|undefined reference to `RomanType::getrom(char*)'|
D:\programmic++\numeri\main.cpp|18|undefined reference to `RomanType::print() const'|
||=== Build finished: 3 errors, 0 warnings ===|
I can't see what's wrong in my code.
Jan 2, 2013 at 11:38pm UTC
I see only that one function is defined inside another function
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
int RomanType::getnum(char str[])
{
for (int i=0; i<SIZE; i++)
{
switch (rom[i])
{
case 'M' :
num =+ 1000;
break ;
case 'D' :
num =+ 500;
break ;
case 'C' :
num =+ 100;
break ;
case 'L' :
num =+ 50;
break ;
case 'X' :
num =+ 10;
break ;
case 'V' :
num =+ 5;
break ;
case 'I' :
num =+ 1;
break ;
default :
cout << "Invalid input" ;
break ;
}
}
void RomanType::print() const
{
cout << num;
cout << rom;
}
}// <== is it a typo?
It it is a typo then it looks like that object file of RomanType.cpp was not included in the project.
Last edited on Jan 2, 2013 at 11:39pm UTC
Jan 3, 2013 at 10:03am UTC
Yes, it was a typo. I corrected the code with your hints but it still doesn't work (same error statements).
Jan 3, 2013 at 10:09am UTC
Undefined reference means the linker couldn't find the implementation of those functions. Make sure RomanType.cpp is being compiled and linked by your toolchain. (If you're using an IDE, make sure it's included in the project.)
Jan 3, 2013 at 10:17am UTC
Thanks a lot! I linked it properly and now it works!
Topic archived. No new replies allowed.