C++ 尝试创建对象并将其打印到输出
嗨,我有一个多项式类,我试图打印多项式到屏幕上,但有问题。我创建了一个方法,在该方法中对象(多项式)打印,但当我尝试从方法外部打印它时,它没有打印,这使我相信我没有正确地创建对象。当我在main中包含create()方法时,对象将自己打印到屏幕上,但当我使用printscreen()方法时,它不会打印。任何帮助都将不胜感激 我将重新发布整个代码以避免任何混淆C++ 尝试创建对象并将其打印到输出,c++,visual-studio-2008,C++,Visual Studio 2008,嗨,我有一个多项式类,我试图打印多项式到屏幕上,但有问题。我创建了一个方法,在该方法中对象(多项式)打印,但当我尝试从方法外部打印它时,它没有打印,这使我相信我没有正确地创建对象。当我在main中包含create()方法时,对象将自己打印到屏幕上,但当我使用printscreen()方法时,它不会打印。任何帮助都将不胜感激 我将重新发布整个代码以避免任何混淆 #include "stdafx.h" #include <vector> #include <
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
class Polynomial
{
private:
int coef[100];
int deg;
public:
Polynomial::Polynomial()
//~Polynomial(void);
{
for ( int i = 0; i < 100; i++ )
{
coef[i] = 0;
}
}
void set ( int a , int b )
{
coef[b] = a;
deg = degree();
}
int degree()
{
int d = 0;
for (int i = 0; i < 100; i++ )
if ( coef[i] != 0 ) d = i;
return d;
}
void print()
{
for ( int i = 99; i >= 0; i-- ) {
if ( coef[i] != 0 ) {
cout << coef[i] << "x^" << i << " ";
}
}
}
void reset()
{
for ( int i = 99; i >= 0; i-- ) {
if ( coef[i] != 0 ) {
coef[i] = 0;
}
}
}
int count()
{
int ct = 0;
for ( int i = 99; i >= 0; i-- ) {
if (coef[i] != 0 ) {
ct++;
return ct;
}
}
}
Polynomial plus ( Polynomial b )
{
Polynomial a = *this;
Polynomial c;
for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
for ( int i = 0; i <= b.deg; i++ ) c.coef[i] += b.coef[i];
c.deg = c.degree();
return c;
}
Polynomial minus ( Polynomial b )
{
Polynomial a = *this; //a is the poly on the L.H.S
Polynomial c;
for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i];
c.deg = c.degree();
return c;
}
void printscreen () const
{
//Polynomial a;
std::cout << "Your polynomial is ";
this->print();
}
Polynomial create ()
{
int temp = 0;
int terms = 0;
int exp = 0;
int n = 0;
Polynomial a = *this;
cout << "How many terms would you like there to be in your polynomial?";
cin >> terms;
for ( int i = 1; i <= terms; i++ ){
n++;
cout << "\n";
cout << "What would you like to be your coefficient #"; cout << n; cout << "?";
cin >> temp;
cout << "What would you like to be your exponent #"; cout << n; cout << "?";
cin >> exp;
a.set ( temp, exp );
}
cout << "Your polynomial is "; a.print();
return a;
}
};
void menu (void)
{ cout<<endl<<endl;
cout<<"What would you like to do next? Please select from the following menu:" <<endl;
cout<<"1. Create another polynomial"<<endl;
cout<<"2. Reset the polynomial"<<endl;
cout<<"3. Display the number of terms in the polynomial"<<endl;
cout<<"4. Sum the polynomials"<<endl;
cout<<"5. Print the polynomial"<<endl;
cout<<"6. Quit"<<endl<<endl;
cout<<"Enter selection>";
}
int _tmain(int argc, _TCHAR* argv[])
{
Polynomial a, b, c;
int choice;
a.create();
do {
menu();
cin>>choice;
switch (choice) {
case 1 :a.create();
cout<<endl;
break;
case 2 : a.reset();
cout<<endl<<"The polynomial has been reset."<<endl;
break;
case 3: cout<<endl<<"Length is ";//<<a.length()<<endl;
break;
case 4: cout<<endl<<"Sum is "; a.plus(b);//<<s.sum()<<endl;
break;
case 5: printscreen(a);
break;
case 6: cout<<endl<<"Thanks and goodbye."<<endl;
break;
default : cout<<endl<<"Invalid command. Try again."<<endl;
}
} while (choice != 6);
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
类多项式
{
私人:
int coef[100];
内度;
公众:
多项式::多项式()
//~多项式(空);
{
对于(int i=0;i<100;i++)
{
coef[i]=0;
}
}
无效集(整数a,整数b)
{
coef[b]=a;
deg=度();
}
int度()
{
int d=0;
对于(int i=0;i<100;i++)
如果(coef[i]!=0)d=i;
返回d;
}
作废打印()
{
对于(int i=99;i>=0;i--){
if(coef[i]!=0){
不能使用
那么,在create()
中,您的意思是:
{
Polynomial a;
//...
a.set (temp, exp);
//...
a.print();
}
另一方面,在printscreen()
中,您的意思是
{
Polynomial a;
a.print();
}
显然,建立多项式的关键步骤完全缺失了
所有这些设置都应该放在多项式
类的成员函数中,而不是放在自由函数中
顺便说一下,C++中没有术语“方法”,相反,我们更喜欢说“成员函数”,但是在你的问题中似乎没有定义任何成员函数。
如果我猜测上下文错误,并且所有这些实际上都是成员函数,那么将printscreen()
更改为:
void printscreen() const // in global scope this is "Polynomial::printscreen()"
{
std::cout << "Your polynomial is ";
this->print();
}
void printscreen()const//在全局范围内,这是“多项式::printscreen()
{
std::cout print();
}
添加多项式::打印方法怎么样?你能让代码更整洁一点,这样更可读吗?另外,你有一个printscreen
函数,但你没有显示你的打印函数。嗯,我怀疑所有这些都是类中的成员函数…所以可能应该是this->print()
或类似的内容。是的,它们是成员函数。我按照您的建议执行了操作,我遇到了两个错误:错误C2662:Polyman::print:无法将“this”指针从“const polyman”转换为“polyman&”,第二个错误是错误C3861“printscreen:“找不到标识符。您的打印()成员函数需要是常量,也就是说,void polymonal::print()const
可以清除一个错误,但它仍然给我一个错误,即没有找到“printscreen:”标识符对不起,这是我的打印函数:void print(){for(int i=99;i>=0;i--){if(coef[i]!=0){cout
{
Polynomial a;
a.print();
}
void printscreen() const // in global scope this is "Polynomial::printscreen()"
{
std::cout << "Your polynomial is ";
this->print();
}