Class .h .cpp undefined reference understanding problem

Hi

I'm learning cpp by myself and get stuck on classes. I have read some stuff on this forum and my cpp book, but still didn't figure out where is the problem.

Here my code in 3 files:

1
2
3
4
5
6
7
8
9
10
11
12
13
//main2.cpp
#include <iostream>
#include <iomanip>
#include "klasy.h"

using namespace std;

int main()
{
	Submarine s;	
	
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//klasy.h
#ifndef KLASY_H
#define KLASY_H 

class Submarine
{
public:
Submarine(int = 0, int = 0, int = 0, int = 0);
private:
	int id_area;
	int speed_max;
	int speed_left;
	int range;
public:
	void resetSpeed();
	void setMaxSpeed(int);
	void moveToArea(int);
};

#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
//klasy.cpp
#include <iostream>
#include <iomanip>
#include "klasy.h"

Submarine::Submarine(int i1, int 12, int 13, int i4) 
{
	id_area=;i1
	speed_max=i2;
	speed_left=i3;
	range=i4;
}
void Submarine::resetSpeed()
{
	speed_left=speed_max;	
}
void Submarine::setMaxSpeed(int sp);
{
	speed_max=sp;
}
void Submarine::moveToArea(int i)
{
	id_area=i;
}




When im trying to compile it:
$ g++ -o main2 main2.cpp

machine says:
/tmp/ccmIhNGI.o: In function `main':
main2.cpp:(.text+0x31): undefined reference to `Submarine::Submarine(int, int, int, int)'
collect2: error: ld returned 1 exit status
You must compile all source file (.cpp).

g++ -o main2 main2.cpp klasy.cpp
thanks Peter for fast answer.

result is as below:

$ g++ -o main2 main2.cpp klasy.cpp
klasy.cpp:6:34: error: expected ‘,’ or ‘...’ before numeric constant
klasy.cpp:6:1: error: prototype for ‘Submarine::Submarine(int, int)’ does not match any in class ‘Submarine’
In file included from klasy.cpp:4:0:
klasy.h:5:7: error: candidates are: Submarine::Submarine(const Submarine&)
klasy.h:8:1: error: Submarine::Submarine(int, int, int, int)
klasy.cpp:17:35: error: declaration of ‘void Submarine::setMaxSpeed(int)’ outside of class is not definition [-fpermissive]
klasy.cpp:18:1: error: expected unqualified-id before ‘{’ token
I have removed ";" from line 17 of klasy.cpp, but it didn't help
Its because of the semicolon right before the body of the function definition of maxspeed.
And the semicolon at line 8 of klasy.cpp
thanks Lorence

output is as below:

$ g++ -o main2 main2.cpp klasy.cpp
klasy.cpp:6:34: error: expected ‘,’ or ‘...’ before numeric constant
klasy.cpp:6:1: error: prototype for ‘Submarine::Submarine(int, int)’ does not match any in class ‘Submarine’
In file included from klasy.cpp:4:0:
klasy.h:5:7: error: candidates are: Submarine::Submarine(const Submarine&)
klasy.h:8:1: error:                 Submarine::Submarine(int, int, int, int)


corrected sourse files are:

1
2
3
4
5
6
7
8
9
10
11
12
13
//main2.cpp
#include <iostream>
#include <iomanip>
#include "klasy.h"

using namespace std;

int main()
{
	Submarine s;	
	
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//klasy.h
#ifndef KLASY_H
#define KLASY_H 

class Submarine
{
public:
Submarine(int = 0, int = 0, int = 0, int = 0);
private:
	int id_area;
	int speed_max;
	int speed_left;
	int range;
public:
	void resetSpeed();
	void setMaxSpeed(int);
	void moveToArea(int);
};

#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
//klasy.cpp
#include <iostream>
#include <iomanip>
#include "klasy.h"

Submarine::Submarine(int i1, int 12, int 13, int i4) 
{
	id_area=i1;
	speed_max=i2;
	speed_left=i3;
	range=i4;
}
void Submarine::resetSpeed()
{
	speed_left=speed_max;	
}
void Submarine::setMaxSpeed(int sp)
{
	speed_max=sp;
}
void Submarine::moveToArea(int i)
{
	id_area=i;
}
On line 6 in klasy.cpp you use 1 instead of i in the names of the second and third parameter.
wow, thats really embarrassing.
problem solved, thanks guys :)
Topic archived. No new replies allowed.