VS2015编写标头和源文件时的编译时错误

时间:2015-07-31 15:38:21

标签: c++ visual-studio-2015

我的VS2015有问题。当我编写一个标题,然后我编写它的源文件并将它们放入项目中时,当我尝试编译它时会给我带来很多错误,但是,如果我把所有内容放在主文件中,它就能完美地运行。

我错过了什么吗?

我正在发帖头和源文件。

谢谢!

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;
}

错误:

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

它似乎跳过了Header.h,但我不知道为什么。

谢谢。

1 个答案:

答案 0 :(得分:1)

如果您的项目配置为使用预编译头文件(默认),则需要将stdafx.h文件作为项目中每个.cpp文件的第一个#include包含。

因此,要修复您的特定方案,请将Header.cpp文件中的#include“stdafx.h”行移至顶部,或者将项目配置为不使用预编译的标题。