Linking errors

Hi all,

This code cannot compile because of the following errors:

main.cpp|12|undefined reference to `Scheme::Scheme(std::string, short, short, short)'
main.cpp|15|undefined reference to `Scheme::parse_input(int, char**)'

I checked if the names matched, but the problem seems to lie somewhere else.
The relevant code is:

main.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
#include <iostream>
#include <string>
#include "error_manager.h"
#include "parser.h"
#include "filereader.h"

int main(int argc, char* argv[])
{

	// parameters which determine program running scheme
	Scheme param;

	// parse given input and initialize variables correctly
	param.parse_input(argc, argv);

	// attempt to read files, return error if failure occurs
	try {
			DataFile data = read_files(param.filename);
			std::cout << data.table[2][2] << "\n"; // test
			// run_prog(param, data);
	}
	catch (int e) {
			show_error(e);
			return e;
	}

	return 0;
}


parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// manages parsing
#pragma once

#include <iostream>
#include <string>

class Scheme {

public:

	Scheme(std::string filename = "ta031", short pivot = 0, short neighbour = 0, short init = 0);

	std::string filename;
	short pivot;
	short neighbour;
	short init;

	// this will parse the received input
	void parse_input(int count, char* arg[]);

};


parser.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
// manages parsing
#pragma once

#include <iostream>
#include <string>


// struct for init data
class Scheme {

public:

	Scheme(std::string filename = "ta031", short pivot = 0, short neighbour = 0, short init = 0);

	std::string filename;
	short pivot;
	short neighbour;
	short init;

	// this will parse the received input
	void parse_input(int count, char* arg[]) {

		if (count<2) {
			std::cout << "Note that you can add arguments in the cmd\n";
			std::cout << "Check the help file for more information.\n";
		}

		else {

			for (int i=1; i<count; i++) {

				std::string str(arg[i]);

				if (str.substr(0,2) == "++") {
					filename = str.substr(2);
				}

				else if (str.substr(0,2) == "--") {

						 if (str.substr(2) == "first")	pivot=0;
					else if (str.substr(2) == "best")	pivot=1;

					else if (str.substr(2) == "transpose")	neighbour=0;
					else if (str.substr(2) == "exchange")	neighbour=1;
					else if (str.substr(2) == "insert")		neighbour=2;

					else if (str.substr(2) == "random-init")	init=0;
					else if (str.substr(2) == "neh")			init=1;

					else std::cout << "Warning: unkown parameter '" << str << "'\n";
				}

				else {
					std::cout << "Warning: invalid input '" << str << "'\n";
				}
			}

		}


	};

};


Does anyone see the error ?
Thanks

(this is compiled with gcc)
1
2
g++ -c *.cpp #compile
g++ *.o -o program.bin #link 


Instead of redeclaring your struct, just include the header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//parser.cpp
//#pragma once //you would not include cpps
#include "parser.h"

void Scheme::parse_input(int count, char* arg[]) {
//...
}

Scheme::Scheme(std::string filename, short pivot, short neighbour, short init)://you never defined this
    filename(filename), 
    pivot(pivot), 
    neighbour(neighbour), 
    init(init)
{}
Last edited on
Thank you ! that was helpful.

just one more thing: in your code snippet, the compiler insists that:
parser.cpp|9|error: expected ')' before 'filename'

yet the code seems syntactically correct.
Any ideas why it shows an error ?

This is because filename is already a member of Scheme. You need to use a different name. I like to put a '_' in front of the variable name in my function argument list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//parser.cpp
//#pragma once //you would not include cpps
#include "parser.h"

void Scheme::parse_input(int count, char* arg[]) {
//...
}

Scheme(std::string _filename = "ta031", short _pivot = 0, short _neighbour = 0, short _init = 0):
    filename(_filename), 
    pivot(_pivot), 
    neighbuor(_neighbour), 
    init(_init)
{}
Last edited on
No, that shouldn't matter. It is not in any way ambiguous

I updated the post, it should work now.
I forgot the Scheme:: at the beginning, that tells that it is a method of that class.
Also, default values can't be in declaration and definition. Prefer to put them in the header.
Thanks! that fix solved the error.
Topic archived. No new replies allowed.