So this is my code, I keep getting "unexpected end of file while looking for precompiled header. did you forget to add #include stdafx.h to your source?" so I added it to all headers and files and it still says that same error. How do I get rid of the error?
这是我的代码,我不断地得到“在查找预编译头时意外的文件结束”。您是否忘记添加#include stdafx。h到源文件?"所以我把它添加到所有的头文件中它还是说同样的错误。如何消除误差?
P.S. if you dont mind checking to see if this code answers these questions(The multiplication and division overloading I barely started, but the relational and equality should be ok)
附注:如果你不介意检查一下这段代码是否能回答这些问题(乘法和除法重载我刚开始,但关系和等式应该没问题)
- overload the * multiplication operator
- 重载乘法运算符
- overload the / division operator
- 使/部门操作员超负荷
- overload all the relational and equality operators
- 重载所有关系和等式操作符
Hugeint.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
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
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;
}
2 个解决方案
#1
0
#include "stdafx.h"
must be the first include in every source file that is configured to use the precompiled header (delete your extraneous ;
character by the way). These files must have the -Yu
compilation flag.
# include“stdafx。h"必须是配置为使用预编译头的每个源文件中的第一个包含(删除无关的内容;字符)。这些文件必须有-Yu编译标志。
There must also be a single source file that creates the precompiled header. Traditionally this is called stdafx.cpp
and only contains the single line #include "stdafx.h"
. This file must have the -Yc
compilation flag.
还必须有一个源文件来创建预编译的头文件。传统上这被称为stdafx。cpp中只包含一行#包含“stdafx.h”。这个文件必须有-Yc编译标志。
#2
0
In Visual Studio you could create C++ project -> EmptyProject and than just add main.cpp file with int main() {}
function. In that case all be work fine, and no precompiled headers needed.
在Visual Studio中,您可以创建c++项目——> EmptyProject,而不仅仅是添加main。使用int main()函数的cpp文件。在这种情况下,所有文件都运行良好,不需要预编译的头文件。
#1
0
#include "stdafx.h"
must be the first include in every source file that is configured to use the precompiled header (delete your extraneous ;
character by the way). These files must have the -Yu
compilation flag.
# include“stdafx。h"必须是配置为使用预编译头的每个源文件中的第一个包含(删除无关的内容;字符)。这些文件必须有-Yu编译标志。
There must also be a single source file that creates the precompiled header. Traditionally this is called stdafx.cpp
and only contains the single line #include "stdafx.h"
. This file must have the -Yc
compilation flag.
还必须有一个源文件来创建预编译的头文件。传统上这被称为stdafx。cpp中只包含一行#包含“stdafx.h”。这个文件必须有-Yc编译标志。
#2
0
In Visual Studio you could create C++ project -> EmptyProject and than just add main.cpp file with int main() {}
function. In that case all be work fine, and no precompiled headers needed.
在Visual Studio中,您可以创建c++项目——> EmptyProject,而不仅仅是添加main。使用int main()函数的cpp文件。在这种情况下,所有文件都运行良好,不需要预编译的头文件。