linker problem?

Jun 29, 2012 at 3:01pm
Here is a toy fruit class. If I compile it I get the error:

Undefined symbols for architecture x86_64:
"fruit::fruit(std::string, std::string, float)", referenced from:
_main in cpptest-bglXKK.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I omit lines 48-50 it works. What is happening?

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
#include <iostream>
using namespace std;

class fruit
{
public:
    fruit();
    fruit( string color , string shape , float size );
    string read_color( bool Iprint ) const;
    void change_color( string newcolor );
private:
    string color;
    string shape;
    float size;
};

fruit::fruit()
{
    color = "green";
    shape = "spindle";
    size = 1.2;
}

string fruit::read_color(bool Iprint) const
{
    if ( Iprint == true )
    {
        cout << color << endl;
    }
    return color;
}

void fruit::change_color( string clr )
{
    color = clr;
}


int main(int argc, const char * argv[] )
{
    
    bool Iprint = true;
    fruit fig = fruit();
    string fig_color = fig.read_color( Iprint );
    cout << fig_color << endl;
    
    
    fruit apple = fruit( "red" , "round" , 2.0 );
    string apple_color = apple.read_color( Iprint );
    cout << apple_color << endl;
    
    return 0;
    
}
Jun 29, 2012 at 3:15pm
First of all, you forgot to include the string header, as in #include <string> .

The minor things are:
1) You are using the assignment operator without defining it yourself (a default one is generated anyway).
2) You could write simply int main() if you don't need the command line arguments.
3) The return 0; at the end is not needed (special case for main() function).

Back to assignment, your fruit should rather look like:
1
2
3
fruit fig;
// ...
fruit apple("red", "round", 2.0);


Edit: missing code tags.
Last edited on Jun 29, 2012 at 3:17pm
Jun 29, 2012 at 3:27pm
You forgot define the constructor

fruit( string color , string shape , float size );
Last edited on Jun 29, 2012 at 3:27pm
Jun 30, 2012 at 3:37pm
I still cannot get this to work. It works fine until I add line 46.

Undefined symbols for architecture x86_64:
"fruit::fruit(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float)", referenced from:
_main in cc11pDrQ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

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
#include <iostream>
using namespace std;

class fruit
{
public:
    fruit();
    fruit( string color , string shape , float size );
    string read_color( bool Iprint ) const;
    void change_color( string newcolor );
private:
    string color;
    string shape;
    float size;
};

fruit::fruit()
{
    color = "green";
    shape = "spindle";
    size = 1.2;
}

string fruit::read_color(bool Iprint) const
{
    if ( Iprint == true )
    {
        cout << color << endl;
    }
    return color;
}

void fruit::change_color( string clr )
{
    color = clr;
}


int main( )
{
    
    bool Iprint = true;
    fruit fig;
    string fig_color = fig.read_color( Iprint );

    fruit apple( "red" ,"round" , 2.2);

    
}

Jun 30, 2012 at 3:57pm
Oh, it appears I should add a parametered constructor:

1
2
3
4
5
6
fruit::fruit(string clr , string shp , float size)
{
    color = clr;
    shape = shp;
    size = 2.3;
}

Last edited on Jun 30, 2012 at 3:57pm
Topic archived. No new replies allowed.