【问题标题】:unexpected end of file while looking for precompiled header. did you forget to add #include stdafx.h to your source?查找预编译头文件时意外结束。您是否忘记将#include stdafx.h 添加到您的源代码中?
【发布时间】:2017-06-30 06:24:03
【问题描述】:

所以这是我的代码,我不断收到“在查找预编译头文件时出现意外的文件结尾。您是否忘记将#include stdafx.h 添加到您的源代码中?”所以我将它添加到所有标题和文件中,它仍然说同样的错误。如何摆脱错误?

附:如果你不介意检查这段代码是否回答了这些问题(乘法和除法重载我才刚开始,但关系和相等应该没问题)

  • 重载 * 乘法运算符
  • 重载 / 除法运算符
  • 重载所有关系和相等运算符

Hugeint.h

#ifndef HUGEINT_H
#define HUGEINT_H

#include <array>
#include <iostream>
#include <string>
#include "stdafx.h";

class Hugeint
{
friend std::ostream &operator<<(std::ostream &, const Hugeint &);
public:
static const int digits = 30; 

Hugeint(long = 0); 
Hugeint(const std::string &); 


Hugeint operator+(const Hugeint &) const;
Hugeint operator+(int) const;
Hugeint operator+(const std::string &) const;

Hugeint operator*(const Hugeint &) const;
Hugeint operator/(const Hugeint &) const;

//relation
bool operator<(const Hugeint &) const;
bool operator<=(const Hugeint &) const;
bool operator>(const Hugeint &) const;
bool operator>=(const Hugeint &) const;
//equal
bool operator==(const Hugeint &) const;
bool operator!=(const Hugeint &) const;


private:
std::array< short, digits > integer;
//short arrayInt[ max ];
}; 

#endif

Hugeint.cpp

#include <cctype>
#include "HugeInt.h"
#include "stdafx.h"
using namespace std;


Hugeint::Hugeint(long value)
{

for (int i = 0; i<digits; i++)
    integer[i] = 0;


for (size_t j = digits - 1; value != 0 && j >= 0; --j)
{
    integer[j] = value % 10;
    value /= 10;
} 
} 

Hugeint::Hugeint(const string &number)
{

for (int i = 0; i<digits; i++)
    integer[i] = 0;

size_t length = number.size();

for (size_t j = digits - length, k = 0; j < digits; ++j, ++k)
    if (isdigit(number[k])) 
        integer[j] = number[k] - '0';

} 

Hugeint Hugeint::operator+(const Hugeint &op2) const
{
Hugeint temp; 
int carry = 0;

for (int i = digits - 1; i >= 0; --i)
{
    temp.integer[i] = integer[i] + op2.integer[i] + carry;

    if (temp.integer[i] > 9)
    {
        temp.integer[i] %= 10; 
        carry = 1;
    }
    else 
        carry = 0;
} 

return temp; 
} 


Hugeint Hugeint::operator+(int op2) const
{

return *this + Hugeint(op2);
} 

Hugeint Hugeint::operator+(const string &op2) const
{

return *this + Hugeint(op2);
} 



bool Hugeint::operator<(const Hugeint &hugeInt) const
{
bool less = false;
for (int i = digits - 1; i >= 0 && !less; --i)
{

    if (integer[i] <hugeInt.integer[i])
    {
        less = true;
    } 
} 

return less;


}

bool Hugeint::operator<=(const Hugeint &hugeInt) const
{
bool LessThan = false;
for (int i = digits - 1; i >= 0 && !LessThan; --i)
{

    if (integer[i] <= hugeInt.integer[i])
    {
        LessThan = true;
    } 

} 

return LessThan; 
}

bool Hugeint::operator>(const Hugeint &hugeInt) const
{
bool greater = false;
for (int i = digits - 1; i >= 0 && !greater; --i)
{

    if (integer[i] >hugeInt.integer[i])
    {
        greater = true;
    } 

} 

return greater; 
}

bool Hugeint::operator>=(const Hugeint &hugeInt) const
{
bool GreaterThan = false;
for (int i = digits - 1; i >= 0 && !GreaterThan; --i)
{

    if (integer[i] >= hugeInt.integer[i])
    {
        GreaterThan = true;
    }

} 

return GreaterThan; 

}

bool Hugeint::operator==(const Hugeint &hugeInt) const
{
bool eqaul = true;
for (int i = digits - 1; i >= 0 && eqaul; --i)
{

    if (integer[i] != hugeInt.integer[i])
    {
        eqaul = false;
    }

} 

return eqaul; 
}


ostream& operator<<(ostream &output, const Hugeint &num)
{
size_t i;

for (i = 0; (i < Hugeint::digits) && (0 == num.integer[i]); ++i); 

if (i == Hugeint::digits)
    output << 0;
else
    for (; i < Hugeint::digits; ++i)
        output << num.integer[i];

return output;
} 

main.cpp

#include<iostream>
#include<string>
#include "Hugeint.h"
#include "stdafx.h"

using namespace std;


int main(){
char value1[30];
char value2[30];

cout << "enter HugeInt1 : ";
cin >> value1;

Hugeint hugeInt1(value1);

cout << "enter HugeInt2 : ";
cin >> value2;

Hugeint hugeInt2(value2);

cout << "< opertor " << endl;
if (hugeInt1 < hugeInt2) {
    cout << hugeInt1 << " is less than " << hugeInt2 << endl;
}
else {
    cout << hugeInt1 << " is not less than " << hugeInt2 << endl;
}
cout << "<= opertor " << endl;
if (hugeInt1 <= hugeInt2) {
    cout << hugeInt1 << " is less than or eqaul " << hugeInt2 << endl;
}
else {
    cout << hugeInt1 << " is not less than or equal " << hugeInt2 << endl;
}
cout << "> opertor " << endl;
if (hugeInt1 > hugeInt2) {
    cout << hugeInt1 << " is greater than " << hugeInt2 << endl;
}
else {
    cout << hugeInt1 << " is not greater than " << hugeInt2 << endl;
}
cout << ">= opertor " << endl;
if (hugeInt1 >= hugeInt2) {
    cout << hugeInt1 << " is greater than or eqaul " << hugeInt2 << endl;
}
else {
    cout << hugeInt1 << " is not greater than or equal " << hugeInt2 << 
endl;
}
cout << "== opertor " << endl;
if (hugeInt1 == hugeInt2) {
    cout << hugeInt1 << " is eqaul to " << hugeInt2 << endl;
}
else {
    cout << hugeInt1 << " is not equal to " << hugeInt2 << endl;
}
system("pause");
return 0;

}

【问题讨论】:

标签: c++


【解决方案1】:

#include "stdafx.h" 必须是配置为使用预编译头文件的每个源文件中的第一个包含(顺便删除无关的; 字符)。这些文件必须具有-Yu 编译标志。

还必须有一个创建预编译头文件的源文件。传统上这称为stdafx.cpp,并且只包含单行#include "stdafx.h"。此文件必须具有 -Yc 编译标志。

【讨论】:

    【解决方案2】:

    在 Visual Studio 中,您可以创建 C++ 项目 -> EmptyProject,而不仅仅是添加带有 int main() {} 函数的 main.cpp 文件。在这种情况下,一切都可以正常工作,并且不需要预编译的头文件。

    【讨论】:

      猜你喜欢
      • 2015-04-12
      • 2015-01-14
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多