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:

    #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:

#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:

// 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 = m.round_cent(m);
    cout << m;

    return 0;
}

Errors:

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

It seem that it skips Header.h but I do not know why.

Thank you.

  • That error message is terrifying. It would be very nice if you could try to find relevant stuff in there. – Jashaszun Jul 31 '15 at 15:40
  • Yep it is! It is very strange because, as I said, if I put all in the main cpp file it perfectly runs. I need to improve quite a lot the code then I cannot put all staff in there! – Leonardo Urbano Jul 31 '15 at 15:42
  • In Visual Studio, .CPP files should start with #include "stdafx.h", the precompiled header, as the first line. Also you are using cout and various classes without including iostream and other relevant headers. Unless they are in std_lib_facilities.h. – Banex Jul 31 '15 at 15:43
  • As a general practice, your user-defined header files should appear after the system-defined header files. – unxnut Jul 31 '15 at 15:44
  • I use Stroustrup's header that includes <iostream> and the other relevant headers. For what regards "stdafx.h" it is already on top in main cpp file and even if I put it on top in Header.cpp it does still not work. I have tried to reset settings too but without any result. – Leonardo Urbano Jul 31 '15 at 15:49

If your project is configured to use precompiled headers (default), you need to include the stdafx.h file as the first #include from each .cpp file in the project.

So to fix your specific scenario, either move the #include "stdafx.h" line in your Header.cpp file up to the top, or configure the project not to use precompiled headers.

  • Well spotted. Funny thing that stdafx.h - the compiler ignore everything until it finds that include, irrespective of syntax errors or otherwise. – Niall Aug 6 '15 at 12:01

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.