C++ 程序永远无法通过>>;。那是超负荷的
我正在构建一个名为C++ 程序永远无法通过>>;。那是超负荷的,c++,arrays,operator-overloading,C++,Arrays,Operator Overloading,我正在构建一个名为nim的类,它包含大整数。我的类的标题如下所示: #ifndef NUMAR_INTREG_MARE_H_INCLUDED #define NUMAR_INTREG_MARE_H_INCLUDED #include "array.h" #include <iostream> class nim///nim=numar_intreg_mare { Array a;//the large integer number int semn;//the sig
nim
的类,它包含大整数。我的类的标题如下所示:
#ifndef NUMAR_INTREG_MARE_H_INCLUDED
#define NUMAR_INTREG_MARE_H_INCLUDED
#include "array.h"
#include <iostream>
class nim///nim=numar_intreg_mare
{
Array a;//the large integer number
int semn;//the sign of the number
int n;//the length of the number
public:
nim();
nim(nim const &ob);
~nim();
friend std::ostream& operator<<(std::ostream& os, const nim& ob);
friend std::istream& operator>>(std::istream& is, nim& ob);
};
#endif // NUMAR_INTREG_MARE_H_INCLUDED
#include <iostream>
#include "array.h"
#include "numar_intreg_mare.h"
using namespace std;
int main()
{
nim nr;
cin>>nr;
cout<<"gets past cin";
cout<<nr;
return 0;
}
我构建这个类是为了能够动态分配内存,正如您从Append
的工作原理中所看到的:
#include "array.h"
#include <iostream>
Array::Array()
{
capacity=1;
c=new int[capacity];
len=0;
}
void Array::Append(int x)
{
int* temp;
if(len>=capacity)
{
temp=new int[2*capacity];
for(int i=0; i<capacity; i++)
{
temp[i]=c[i];
}
capacity*=2;
delete[] c;
c=temp;
}
c[len]=x;
len++;
}
Array::~Array()
{
delete[]c;
}
int& Array::operator[] (int x) const
{
return c[x];
}
void Array::Afisare()
{
for(int i=0; i<len; i++)
std::cout<<c[i]<<" ";
std::cout<<"\n";
}
现在,当我的main.cpp
如下所示:
#ifndef NUMAR_INTREG_MARE_H_INCLUDED
#define NUMAR_INTREG_MARE_H_INCLUDED
#include "array.h"
#include <iostream>
class nim///nim=numar_intreg_mare
{
Array a;//the large integer number
int semn;//the sign of the number
int n;//the length of the number
public:
nim();
nim(nim const &ob);
~nim();
friend std::ostream& operator<<(std::ostream& os, const nim& ob);
friend std::istream& operator>>(std::istream& is, nim& ob);
};
#endif // NUMAR_INTREG_MARE_H_INCLUDED
#include <iostream>
#include "array.h"
#include "numar_intreg_mare.h"
using namespace std;
int main()
{
nim nr;
cin>>nr;
cout<<"gets past cin";
cout<<nr;
return 0;
}
#包括
#包括“array.h”
#包括“numar_intreg_mare.h”
使用名称空间std;
int main()
{
nim-nr;
cin>>天然橡胶;
难道你永远不会使用Array::Append()
函数,所以操作符>
中的ob.a.c
数组的长度始终等于1。这与您的问题无关,但在输入操作符中有输出是不好的形式。对于某些输入,您也从std::cin
读取,而不是作为参数传递的流。@Yksisarvinen这是我的OOP课程,我们不允许使用它现在的模板。@Someprogrammerdude如何从istream输入?