Visual Studio Compile-time Error with Header and Source File

Hi!

I have a problem with VS2015. When I write a header and then I write its source file and put them in the project, when I try to compile it gives me a lot of errors but, if I put everything in the main file, it works perfectly.

Do I miss something?

I'm posting header and source file.

Thanks!

Header.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

class Money
{
private:
	int dollar;
	long int cents;
public:
	Money();
	Money(int d, long int c);
	int f_dollar() const { return dollar; }
	int f_cents() const { return cents; }
	Money round_cent(Money& m);
};

ostream& operator<<(ostream& os, const Money& m);
istream& operator>>(istream& is, Money& m);


Header.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
#include "Header.h"
#include "std_lib_facilities.h"
#include "stdafx.h"

Money::Money()
	:dollar{ 0 }, cents{ 0 } {};
Money::Money(int d, long int c)
	:dollar{ d }, cents{ c } {};

Money Money::round_cent(Money& m)
{
	int last_digit = m.cents % 10;
	int diff = 10;
	if (last_digit >= 5)
	{
		diff -= last_digit;
		return Money(m.dollar, m.cents + diff);
	}
	if (last_digit < 5) return Money(m.dollar, m.cents - last_digit);
}

istream& operator>>(istream& is, Money& m)
{
	int dollar;
	long int cents;
	cout << "Type dollars amount: $" << endl;
	is >> dollar;
	is.ignore(10, '.');
	is >> cents;
	if (!is) return is;

	m = Money(dollar, cents);

	return is;
}

ostream& operator<<(ostream& os, const Money& m)
{
	return os << "We have " << m.f_dollar() << "." << m.f_cents()
		<< " rounded." << endl;
}


Money.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Money.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "std_lib_facilities.h"
#include "Header.h"


int main()
{
	Money m;
	cin >> m;
	m.round_cent(m);
	cout << m;

    return 0;
}


Severity Code Source Description Project File Line
Error C2146 Build syntax error: missing ';' before identifier 'round_cent' Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 10
Error C2143 Build syntax error: missing ';' before '{' Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 11
Error C2143 Build syntax error: missing ';' before '{' Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 38
Error C2143 Build syntax error: missing ';' before '&' Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 22
Error C2143 Build syntax error: missing ';' before '&' Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 37
Error C2059 Build syntax error: 'const' Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 37
Error C4430 Build missing type specifier - int assumed. Note: C++ does not support default-int Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 6
Error C4430 Build missing type specifier - int assumed. Note: C++ does not support default-int Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 8
Error C4430 Build missing type specifier - int assumed. Note: C++ does not support default-int Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 10
Error C4430 Build missing type specifier - int assumed. Note: C++ does not support default-int Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 22
Error C4430 Build missing type specifier - int assumed. Note: C++ does not support default-int Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 37
Error C2563 Build mismatch in formal parameter list Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 22
Error C2447 Build '{': missing function header (old-style formal list?) Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 11
Error C2447 Build '{': missing function header (old-style formal list?) Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 38
Error C2065 Build 'os': undeclared identifier Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 37
Error C2473 Build 'operator >>': looks like a function definition, but there is no parameter list. Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 22
Error C2365 Build 'Money': redefinition; previous definition was 'function' Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 10
Error C2653 Build 'Money': is not a class or namespace name Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 5
Error C2653 Build 'Money': is not a class or namespace name Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 7
Error C2653 Build 'Money': is not a class or namespace name Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 10
Warning C4508 Build 'Money': function should return a value; 'void' return type assumed Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 6
Warning C4508 Build 'Money': function should return a value; 'void' return type assumed Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 8
Error C2550 Build 'Money': constructor initializer lists are only allowed on constructor definitions Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 6
Error C2550 Build 'Money': constructor initializer lists are only allowed on constructor definitions Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 8
Error C2065 Build 'm': undeclared identifier Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 22
Error C2065 Build 'is': undeclared identifier Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 22
Error C2448 Build '>>': function-style initializer appears to be a function definition Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 23
Warning C4627 Build '#include "std_lib_facilities.h"': skipped when looking for precompiled header use Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 2
Warning C4627 Build '#include "Header.h"': skipped when looking for precompiled header use Money c:\users\leonardo\documents\visual studio 2015\projects\money\money\header.cpp 1
Last edited on
You missed an <iostream> include at the top of Money.h.
You also need to change from ostream to std::ostream and istream to std::istream, at least in the header.
There are several problems with your code.

First in your header file you are trying to use standard C++ classes that are defined within the std namespace without properly scoping them. The ostream and istream classes need to be scoped using the scope resolution operator:: (std::ostream).

Next in your header.cpp file you have a couple of "extra" semicolons for your constructor implementations.

Also it is possible that your header files are order dependent. The <stdafx.h> inclusion should probably be first, followed by any C/C++ standard headers, followed by that "std_lib_facilities.h", and lastly your header. That std_lib_facilities.h include does some modifications of some standard classes so it needs to be after the standard headers. It also has a using statement so it should be before your header as well.

You would probably be better off turning off the use of precompiled headers and removing the stdafx.h header inclusion.
I have solved putting "Header.h" after "stdafx.h" and "std_lib_facilities.h" :)
Last edited on
Topic archived. No new replies allowed.