Problem with classes and linking

I keep getting the error "In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status". Something is wrong and I am clueless as to what the problem is. In Xcode it says "linker command failed with exit code 1 (use -v to see invocation)" and that there is a duplicate symbol color and a duplicate symbol print_iii(im guessing the print function). Its kinda frustrating. Really need some help. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//My .h file
#ifndef COLOR_H
#define COLOR_H
class Color {
 public:
	int red;
	int green;
	int blue;
	inline void setRed(int red);
	inline void setGreen(int green);
	inline void setBlue(int blue);
	inline int getRed();
	inline int getGreen();
	inline int getBlue();   
};
#endif 


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
// my .cpp file that compiles and makes a .o file
#include <iostream>
#include <cstdlib>
#include "color.h"
using namespace std;
void print (int, int, int);
Color color;
void Color::setRed (int red){
    int x = 0;
    color.red = x;
    cout << "Enter only numbers 1-255" << endl;
    cout << "Enter the red level: " << endl;
    cin >> color.red;
    if (color.red < 0 || color.red > 255){
        cout << "You entered a number that is not allowed! Start over!"<< endl;
        exit (1);
    }
}
void Color::setGreen (int green){
    int y = 0;
    color.green = y;
    cout << "Enter the green level: " << endl;
    cin >> color.green;
    if (color.green < 0 || color.green > 255){
        cout << "You entered a number that is not allowed! Start over!"<< endl;
        exit (1);
    }
}
void Color::setBlue (int blue){
    int z = 0;
    color.blue = z;
    cout << "Enter the blue level: " << endl;
    cin >> color.blue;
    if (color.blue < 0 || color.blue > 255){
        cout << "You entered a number that is not allowed! Start over!"<< endl;
        exit (1);
    }
}
int Color::getRed(){return color.red;}
int Color::getGreen(){return color.green;}
int Color::getBlue(){return color.blue;}

void print(int red, int blue, int green){
    if ((color.red == 0) && (color.green == 0) && (color.blue == 0))
        cout << "Color: black ";
    if ((color.red == 255) && (color.green == 255) && (color.blue == 255))
        cout << "Color: white ";
    if (color.red == 128 && color.green == 128 && color.blue == 128)
        cout << "Color: grey ";
    else
        cout << "#" << color.red << "-" << color.green << "-" << color.blue << endl;
        cout << hex << uppercase << "hex - #" << color.red << "-" << color.green << "-" << color.blue << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//and my main file
#include <iostream>
#include <cstdlib>
#include "colors.h"
using namespace std;
int main(){
    int x =0, y=0, z=0;
    color.setRed(x);
    color.setGreen(y);
    color.setBlue(z);
    color.getRed();
    color.getGreen();
    color.getBlue();
    print(x, y, z);
    
    return 0;
}
My guess is that your linker does not know where color and print are defined

1. Try moving Color color; from your cpp to your main.
2. Define print function in the header file
It solved the colors part (i had to get rid of the color.blah parts). but the print function is still having problems
But in g++ it says "'Colors' has not been declared" so in xcode it works but g++ it doesnt
Move this to color.h:
void print (int, int, int);
Already did. Still doesn't work.
Is color a global variable ? As it is your code, this is required.
didn't make a difference really. the biggest problem is the print function. it just won't work for some reason.
So I solved it by placing print() inline and by making the variables in the class functions in class reference variables. however in g++ im still having problems. It keeps saying
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

What does it mean by undefined ref in main. This works in xcode but in g++ it doesn't. Any ideas?
void print(int red, int blue, int green) should be replaced by void print(Color color) in both cpp and h files, and in main use print(color). Alternatively, define print as a function in the Color class, and call it from the main as color.print().
Otherwise your print function does not use red, green, or blue, and instead tries to deal with the global object color
Also, make sure you are linking all the files in g++
Last edited on
I solved it. I was linking it right but in inline functions I was supposed to define them in the class so it was giving me errors. With a few other tweaks here and there and it is now running properly.
Topic archived. No new replies allowed.