Getting these errors when making

closed account (i8bjz8AR)
In function `main':
run.cpp:(.text+0x5): undefined reference to `test_default_1()'
collect2: error: ld returned 1 exit status
make[2]: *** [run] Error 1

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
#include <iostream>
#include <string>

void test_default_1();


struct Regular_type
{
  Regular_type()
  {
    // TODO: Modify this as required by the instructions.
    std::string s1 = "default ctor";
    std::cout << this << std::endl;
  }

  ~Regular_type()
  {

    // TODO: Modify this as required by the instructions.
    std::string s2 = "dtor";
    std::cout << s2 << this << std::endl;
  }

  void f()
  {
    // TODO: Modify this as required by the instructions.

    std::string s3 = "member";
    std::cout << s3 << this << std::endl;
  }

  void test_default_1()
  {
    Regular_type a;
    Regular_type b;
  }
};


int
main()
{


test_default_1(); //calling the  first function as requested

}
Last edited on
Line 4: You declare test_default_1 to be a global function.

Lines 32-36: test_default_1 is a member function of Regular_type.

Which is it? Those are two different functions are far as the linker is concerned.

You did not provide an implementation of the global function, which is why you received the linker error.


closed account (i8bjz8AR)
okay so now i'm getting this error, changed a few things, 49:16: error: ‘test_default_1’ was not declared in this scope
test_default_1(); //calling the first function as requested

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
#include <iostream>
#include <string>

//void test_default_1(string, string, string);


struct Regular_type
{
  Regular_type()
  {

    // TODO: Modify this as required by the instructions.
    std::string s1 = "default ctor";
    std::cout << this << std::endl;
  }

  ~Regular_type()
  {

    // TODO: Modify this as required by the instructions.
    std::string s2 = "dtor";
    std::cout << s2 << this << std::endl;
  }

  void f()
  {
    // TODO: Modify this as required by the instructions.

    std::string s3 = "member";
    std::cout << s3 << this << std::endl;
  }

  void test_default_1()
  {
    Regular_type a;
    Regular_type b;
  }
};


int
main()
{


test_default_1(); //calling the  first function as requested

}
Now that you've commented out line 4, test_default_1 is clearly a member function of class Regular_type.

Line 35-36: Not clear what these lines are supposed to do. As is, this function does nothing.

Line 46: Since test_default_1 is a member function, you have to reference it via an instance of a Regular_type object.



Topic archived. No new replies allowed.