我一直在创建一个类,该类在除10以外的基数中定义一个整数。今天,当我尝试编译时,我在xstring中得到了一长串错误,这些错误似乎主要是 undefined 的类型错误或未声明的标识符错误。我想知道这是由于忘记包含某些文件还是我滥用字符串导致的。
intBase.cpp(我会错误处理字符串的类)
#include "intBase.h"
intBase::intBase(int b)
{
base = b;
num = 0;
}
intBase::intBase(std::string n, int b)
{
base = b;
setNum(n);
}
int intBase::getBase() const
{
return base;
}
void intBase::setBase(int b)
{
base = b;
}
std::string intBase::getNum() const
{
int expo = 0, n, numTemp = num;
std::string result = "";
char ch;
while (num > int(pow(base, expo)))
{
expo++;
}
expo--;
while (expo >= 0)
{
n = int(numTemp / pow(base, expo));
ch = convert(n, false);
numTemp -= int(n * pow(base, expo));
result += ch;
expo--;
}
return result;
}
void intBase::setNum(std::string n)
{
char ch;
int expo, temp;
num = 0;
expo = 0;
while (n.size() > 0)
{
ch = n[n.size() - 1];
temp = convert(ch, true);
num += int(temp * pow(base, expo));
expo++;
n = n.substr(0, n.size() - 1);
}
}
int intBase::getNumTen() const
{
return num;
}
void intBase::setNumTen(int n)
{
num = n;
}
intBase intBase::add(const intBase& iBase) const
{
intBase IB(1);
if (getBase() > iBase.getBase())
{
IB.setBase(base);
}
else
{
IB.setBase(base);
}
IB.setNumTen(getNumTen() + iBase.getNumTen());
return IB;
}
intBase intBase::operator+(const intBase& iBase) const
{
return add(iBase);
}
std::ostream& operator<<(std::ostream& output, const intBase& iBase)
{
output << iBase.getNum();
return output;
}
// takes a character and returns its base ten conversion if bool is true
// takes a base-ten integer and returns its character conversion if bool is false
int intBase::convert(int x, bool letter) const
{
int result;
if (letter)
{
if (x >= 48 && x <= 57)
result = x - 48;
else if (x >= 97 && x <= 122)
result = x - 87;
else if (x >= 65 && x <= 90)
result = x - 29;
else
result = -1;
}
else
{
if (x >= 0 && x <= 9)
result = x + 48;
else if (x >= 10 && x <= 35)
result = x + 87;
else if (x >= 36 && x <= 61)
result = x + 29;
else
result = -1;
}
return result;
}
intBase.h
#pragma once
#include <string>
#include <cmath>
class intBase
{
public:
intBase(int b);
intBase(std::string n, int b);
int getBase() const;
void setBase(int b);
std::string getNum() const;
void setNum(std::string n);
int getNumTen() const;
void setNumTen(int n);
intBase add(const intBase& iBase) const;
intBase operator+(const intBase& iBase) const;
friend std::ostream& operator<<(std::ostream& output, const intBase& iBase);
private:
int convert(int x, bool y) const;
int num, base;
};
测试文件
#include <iostream>
#include "intBase.h"
#include <string>
using namespace std;
int main()
{
int base;
string numTemp;
cout << "enter the base of your number: ";
cin >> base;
intBase num(base);
cout << "enter a number in base " << base << ": ";
cin >> numTemp;
num.setNum(numTemp);
cout << num.getNumTen() << endl;
cout << num << endl;
cout << num + num << endl;
system("pause");
return 0;
}
错误日志
1>------ Build started: Project: numBaseTest, Configuration: Debug Win32 ------
1>intBase.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,23): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::_Insert_string<char,std::char_traits<char>,unsigned int>(std::basic_ostream<char,std::char_traits<char>> &,const _Elem *const ,const _SizeT)' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : with
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : [
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : _Elem=char,
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : _SizeT=unsigned int
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : ]
1>E:\college\numBaseTest\numBaseTest\numBaseTest\intBase.cpp(104): message : see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>,std::allocator<char>>(std::basic_ostream<char,std::char_traits<char>> &,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,1): error C2065: 'iostate': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,31): error C2146: syntax error: missing ';' before identifier '_State'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,31): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,49): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,1): error C2065: 'goodbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(438,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(441,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(444,29): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(444,1): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(444,36): error C2146: syntax error: missing ';' before identifier '_Ok'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(444,36): error C3861: '_Ok': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(446,10): error C2065: '_Ok': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(447,9): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(447,28): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(447,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,39): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,1): error C2065: 'adjustfield': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,64): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,1): error C2065: 'left': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(452,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(452,1): error C2660: 'std::_Narrow_char_traits<char,int>::eq_int_type': function does not take 1 arguments
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(401,38): message : see declaration of 'std::_Narrow_char_traits<char,int>::eq_int_type'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(453,21): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(453,40): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(453,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(459,13): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(459,32): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(459,1): error C2065: 'goodbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(460,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(461,13): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(461,32): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(461,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(464,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(464,1): error C2660: 'std::_Narrow_char_traits<char,int>::eq_int_type': function does not take 1 arguments
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(401,38): message : see declaration of 'std::_Narrow_char_traits<char,int>::eq_int_type'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(465,21): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(465,40): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(465,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(471,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(472,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(472,9): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(472,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(475,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>test.cpp
1>Generating Code...
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(475,20): error C2065: '_State': undeclared identifier
1>Done building project "numBaseTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
谢谢您提前提供的所有帮助。
最佳答案
您缺少要包含的内容。将其添加到intBase.h的顶部:
#pragma once
#include <sstream>
#include <string>
#include <cmath>