Not sure where my issue is

When trying to run my program my errors are:

"Error 8 error C2371: 'Timespan::GetTime : redefinition; different basic types"

"Error 7 error C2556: 'std::string Timespan::GetTime(void) : overloaded function differs only by return type from 'int Timespan::GetTime(void)"

"Error 1 error C2146: syntax error : missing ';' before identifier 'GetTime'"

"Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int"

This assignment is confusing to me as is, just not sure how I messed up already.


Timespan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include "Timespan.h"
#include <string>


using namespace std;

Timespan::Timespan(int hr, int min, int sec)
{
	_hr = hr;
	_min = min;
	_sec = sec;
}

string Timespan::GetTime()
{
	std::string H = to_string(_hr);
	std::string M = to_string(_min);
	std::string S = to_string(_sec);

	return H + ":" + M + ":" + S;
}



Timespan.h
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
#ifndef TIMESPAN_H
#define TIMESPAN_H
#include <string>


class Timespan
{
private:
	int _hr;
	int _min;
	int _sec;

	/*double computeTimespanValue() const;*/
	

public:
	Timespan(int, int, int);
	string GetTime();
	/*void operator = (const Timespan&);
	void operator += (const Timespan&);
	bool operator == (const Timespan&) const;
	bool operator > (const Timespan&) const;
	bool operator < (const Timespan&) const;

	friend double operator * (const Timespan&);
	friend double operator + (const Timespan&);*/
};

#endif 



Main
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
// Week6_1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int hrs = 0;
	int min = 0;
	int sec = 0;

	
	cout<<"Please enter the number of hours: ";
	cin>>hrs;
	cout<<"Please enter the number of minutes: ";
	cin>>min;
	cout<<"Please enter the number of seconds: ";
	cin>>sec;

	Timespan ts1(hrs, min, sec);

	cout << endl << endl;
	cout << "Creating new TimeSpan object called ts1 using the overloaded constructor"
		 << "ts1.GetTime(): " << ts1.GetTime(); 

	cout<<endl;
	system("PAUSE");
	return 0;

}
Last edited on
Those compiler messages should contain also the filename and linenumber.

As is, the errors are hard to link to the code that you did post, particularly when the errors do not even seem to match any part of the code.
"Error 8 error C2371: 'Timespan::GetTime : redefinition; different basic types" Line 15 Timespan.cpp file

"Error 7 error C2556: 'std::string Timespan::GetTime(void) : overloaded function differs only by return type from 'int Timespan::GetTime(void)" Line 15 Timespan.cpp file

"Error 1 error C2146: syntax error : missing ';' before identifier 'GetTime'" Line 18 Timespan.h file

"Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int" Line 18 Timespan.h file
Last edited on
For Error 7 it still says the same thing just without the (int,int,int) parts since I changed my code around.

There, I edited it.
Last edited on
In the h file. you cant use string without using STD.
So either put "using namespace std" or std::string GetTime();

And, the biggest problem, you havent included the class in your main. Put this at the top
#include "Timespan.h"

And that my friend, will fix your errors.
Last edited on
I love you man! (no homo)

I knew my issues were minor, but I was reading into the errors too much. I didn't look to see if all my files were included and such. Just one of those days man. Thanks you!
Happens to the best of us haha :)
Topic archived. No new replies allowed.