Probleme mit Klassendeklaration und Methoden-implementierung

closed account (E3p4z8AR)
Hallo liebe Community,

ich bin neu in C++ und komme aus dem Java-Bereich.
Soweit finde ich mich ganz gut zurecht, nur habe ich Probleme mit der Objektorientierten Erzeugung von Klassen auf eine Header-Datei und eine cpp-Datei verteilt. Tut mir leid für den vielen Code, aber ich bin mittlerweile echt ratlos. Ich hab zahlreiche Tutorials und Foren durchforstet, bin aber nicht auf das Problem in meinem Code gestoßen.

Meine Header-Datei (TimeClock.h):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #pragma once

class TimeClock {
private:
	int hours;
	int minutes;
	int seconds;
public:
	TimeClock();
	~TimeClock();
	TimeClock(TimeFormat format, int minutes);
	TimeClock(int seconds, int minutes, int hours);
	void setSeconds(int seconds);
	void setMinutes(int minutes);
	void setHours(int hours);
	int getMinutes()const;
	int getSeconds()const;
	int getHours()const;
	void tick();
};


Die cpp-Datei(TimeClock.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "pch.h"
#include "TimeClock.h"
#include "TimeFormat.h"

// Constructor

TimeClock::TimeClock() {
	minutes = 0;
	seconds = 0;
	hours = 0;
}

TimeClock::~TimeClock() {

}

TimeClock::TimeClock(TimeFormat format, int time) {
	switch (format) {
		case minute: {
			hours = time > 59 ? int(time / 60) : 0;
			minutes = time % 60;
			break;
		}
		case second: {
			hours = time < 3599 ? int(time / 3600) : 0;
			minutes = time > 59 ? int(time / 60) : 0;
			seconds = time % 60;
			break;
		}
		case hour: {
			hours = time;
			break;
		}
	}
}

TimeClock::TimeClock(int sec, int min, int hrs) {
	setSeconds(sec);
	setMinutes(min);
	setHours(hrs);
}

// Setters

void TimeClock::setSeconds(int sec) {
	seconds = sec;
}

void TimeClock::setMinutes(int min) {
	minutes = min;
}

void TimeClock::setHours(int hrs) {
	hours = hrs;
}

// Getters

int TimeClock::getSeconds() const {
	return seconds;
}

int TimeClock::getMinutes()const {
	return minutes;
}

int TimeClock::getHours()const {
	return hours;
}

// Tick

void TimeClock::tick() {
	if (seconds > 0) {
		seconds--;
	}
	else if (minutes > 0) {
		minutes--;
		seconds = 60;
	}
	else {
		hours--;
		minutes = 60;
	}
}


Enum (Timeformat.h):
1
2
3
4
5
#pragma once

enum TimeFormat {
	second, minute, hour
};

Main-Funktion (Main.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "pch.h"
#include <iostream>
#include "TimeClock.h"
#include "TimeFormat.h"


int main(void) {
	TimeClock time(1, 25, 33);
	//Time time(minute, 55);
	int mins = time.getMinutes();
	int secs = time.getSeconds();
	int hrs = time.getHours();
	std::cout << hrs << " hours, " << mins << " minutes, " << secs << " seconds, " << std::endl;
	return EXIT_SUCCESS;
}


Fehlermeldungen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
1>Main.cpp
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.h(11): error C2061: syntax error: identifier 'TimeFormat'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.h(11): error C2535: 'TimeClock::TimeClock(void)': member function already defined or declared
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.h(9): note: see declaration of 'TimeClock::TimeClock'
1>TimeClock.cpp
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.h(11): error C2061: syntax error: identifier 'TimeFormat'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.h(11): error C2535: 'TimeClock::TimeClock(void)': member function already defined or declared
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.h(9): note: see declaration of 'TimeClock::TimeClock'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.cpp(17): error C2511: 'TimeClock::TimeClock(TimeFormat,int)': overloaded member function not found in 'TimeClock'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.h(3): note: see declaration of 'TimeClock'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.cpp(20): error C2597: illegal reference to non-static member 'TimeClock::hours'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.cpp(21): error C2597: illegal reference to non-static member 'TimeClock::minutes'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.cpp(25): error C2597: illegal reference to non-static member 'TimeClock::hours'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.cpp(26): error C2597: illegal reference to non-static member 'TimeClock::minutes'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.cpp(27): error C2597: illegal reference to non-static member 'TimeClock::seconds'
1>c:\users\tobia\source\repos\learningtimer\learningtimer\timeclock.cpp(31): error C2597: illegal reference to non-static member 'TimeClock::hours'
1>Generating Code...
1>Done building project "LearningTimer.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Vielen Dank schon einmal für eure Hilfe!
TimeClock(TimeFormat format, int minutes);

At this point in the code, the compiler does not know what TimeFormat is. Put #include TimeFormat.h somewhere before this line.
closed account (E3p4z8AR)
@Repeater

Thank you so much for your answer. You are totally right. I was still looking for mistakes in my cpp file.

Where could you find that out from the error-messages? When i looked at them, i was looking for completely different mistakes.

And should i use "this->" in the implementation of the methods in the cpp-file for accessing the objects variables?
Last edited on
When fixing errors, start with the first one. The compiler works from the top of a translation unit and works downwards; sometimes an early error causes more errors later, so we should start fixing from the first error.

error C2061: syntax error: identifier 'TimeFormat'
was the first error. This error simply means that the compiler does not recognise the token "TimeFormat". I know that it's an enum, because I looked at the code. So I wonder why the compiler doesn't recognise this enum. I look back up in the code to see where the compiler was told about this enum - and I find it was never told about this enum.

And should i use "this->" in

No. You should use it only if you absolutely have to. You will have to if you end up with two different variables with the same name; for example, if you wrote your function like this:

1
2
3
void TimeClock::setSeconds(int seconds) {
	this->seconds = seconds; // this->seconds is the class variable, seconds is the input parameter
}

but if you find yourself doing that, you should stop, and not name your parameters the same as the class variables.

Topic archived. No new replies allowed.