如何在 C++ 中使用主函数之外的对象
How to use an object outside the main function in C++?
如何从主外部访问公共成员函数?当我使用"点运算符"直接调用函数时,它会显示"范围错误"。它表示对象名称未在该作用域下声明。这是我的代码片段。
class laptop
{
private:
int id,price,cpu,gpu,ram,size,qty;
char name[30];
public:
laptop() : id(0),price(0),cpu(0),gpu(0),ram(0),size(0) {}
void add(int i,char n[],int p,int c,int r,int g,int s,int q)
{
id=i; std::strcpy(name,n); price=p; cpu=c; ram=r; gpu=g; size=s; qty=q;
}
int qt()
{
return qty;
}
};
在外部函数(不是成员函数(内部,我将函数调用作为
cout<<l[1].qt();
它只是不断弹出该错误。是的,我是新手!我想它从我的代码中可以自己显示出来。提前感谢!
编辑 - 不确定您要要求哪一部分。所以它来了。我的整个代码:
#include <iostream>
#include <cstring>
#include <iomanip>
//#include <string>
using namespace std;
char name[30],ip3;
int ip1,ip2,i,cart[10][2],temp,id;
static int count=0;
int get_id();
class laptop
{
private:
int id,price,cpu,gpu,ram,size,qty;
char name[30];
public:
laptop() : id(0),price(0),cpu(0),gpu(0),ram(0),size(0) {}
void add(int i,char n[],int p,int c,int r,int g,int s,int q)
{
id=i; std::strcpy(name,n); price=p; cpu=c; ram=r; gpu=g; size=s; qty=q;
}
void disp()
{
cout<<name<<endl<<"Processor-Intel Core i"<<cpu<<" ,RAM-"<<ram<<" GB, "<<gpu<<" GB Graphics, "<<size<<" inches HD display"<<endl;
cout<<"Price Rs."<<price<<endl;
}
void tab()
{
cout<<setw(1)<<id<<setw(22)<<name<<setw(17)<<"Intel Core i"<<cpu<<setw(7)<<ram<<" GB"<<setw(6)<<gpu<<" GB"<<setw(10)<<size<<" inches";
}
int qt()
{
return qty;
}
void add()
{
cout<<l[2].qt();
}
};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main()
{
laptop l[15];
l[1].add(1001,"HP Pavillion 530",30000,5,4,1,15,10);
l[2].add(1002,"HP Pavillion 540",32000,5,6,1,15,11);
l[3].add(1003,"HP Pavillion 730",37000,7,8,2,15,4);
l[4].add(1004,"HP Pavillion 750",44000,7,16,2,17,10);
l[5].add(1005,"HP Pavillion 750+",49000,7,32,4,17,14);
l[6].add(1006,"Dell Vostro DV300",23000,3,4,1,13,2);
l[7].add(1007,"Dell Vostro DV301",25000,3,4,1,14,9);
l[8].add(1008,"Dell Vostro DV510",29000,5,4,1,15,18);
l[9].add(1009,"Dell Vostro DV710",44000,7,8,2,15,4);
l[10].add(1010,"Dell Vostro DV750",48000,7,16,2,17,3);
l[11].add(1011,"Lenovo Y300",27000,3,8,1,13,9);
l[12].add(1012,"Lenovo Y900",60000,7,16,4,17,2);
cout<<"Welcome to Walmart- The online electronic supermarket"<<endl;
cout<<"Walmart is a one-stop destination for all your digital needs"<<endl;
cout<<"Please enter your name"<<endl;
cin>>name;
cout<<"Welcome "<<name<<" to our brand new e-store";
cout<<endl<<"We have laptops,tablets and desktops available in our store currently"<<endl;
cout<<"To go to the laptop store, press 1"<<endl<<"To go to the tablet store, press 2"<<endl<<"To go to the desktop store, press 3";
cin>>ip1;
switch(ip1)
{
case 1:
cout<<"Press 1 for a tabulated view. Else press 2 for individual view";
cin>>ip2;
switch(ip2)
{
case 1:
cout<<"Sl. no."<<setw(7)<<" Model Name"<<setw(20)<<"Processor"<<setw(13)<<"RAM"<<setw(14)<<"Graphics"<<setw(15)<<"Screen Size"<<endl;
for(i=1;i<13;i++)
{
l[i].tab();
cout<<endl;
}
break;
case 2:
cout<<endl<<"Here's a list of all the models available with us"<<endl;
for(i=1;i<13;i++)
{
cout<<i<<") ";
l[i].disp();
cout<<endl;
}
break;
}
cout<<"Are you interested in any of the above listed models? (Y/N)"<<endl;
cin>>ip3;
if(ip3=='N')
{
cout<<"Sorry!Please re visit the store soon for new models.Coming soon!";
break;
}
id=get_id();
cout<<"Please enter the quantity ("<<l[id].qt()<<" available in stock)"<<endl;
cin>>cart[count][1];
if(cart[count][1]<=l[id].qt())
cout<<"Item added to cart";
else
cout<<"Error. Please recheck the quantity";
}
return 0;
}
int get_id()
{
cout<<"Please enter the item id of the model that you are interested in"<<endl;
cin>>cart[count][0];
temp=(cart[count][0])/1000;
if(temp==1)
return (cart[count][0])-1000;
else if(temp==2)
return (cart[count][0])-2000;
}
范围错误意味着您的声明位于错误的位置。 检查声明对象的位置。
变量l
只在函数main
中声明,因此它在main
范围之外不是"可见的"。
你的语句cout<<l[2].qt()
是类 laptop
的成员函数add
的一部分,因此超出了main
的范围:
void add()
{
cout<<l[2].qt();
}
你可能的意思是:
void add()
{
cout << this->qt();
}
相关文章:
- 如何使用单独文件中的派生类访问友元函数对象
- 当使用透明的std函数对象时,我们还需要写空的尖括号吗
- 有没有办法将重载的类函数绑定到函数对象?
- 将指针传递到成员的指针,从模板参数包到函数对象
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- 隐式转换为比较函数对象(函子)用于 std::sort 而不是 std::map?
- C++使用函数对象的线程,如何调用多个析构函数而不是构造函数?
- 如何通过接口将函子分配给函数对象
- 对std::函数对象的调用不匹配,该对象是指向成员函数的指针
- 指向std::invoke中成员函数对象的指针
- 如何成功地将函数对象(或lambda)传递给trackbar回调的第二个参数(void*)
- "std::function"的简单版本:函数对象的生存期?
- C++ 将函数对象作为左值和/或右值传递
- SFINAE 用于具有默认参数的函数对象
- 构造函数对象赋值是否泄漏内存
- 如何发送通过绑定到函数/方法创建的函数对象?
- std::for_each 与函数对象
- 将函数对象传递给 std::function
- 访问执行策略for_each函数对象中的迭代器
- 通过C++函数对象类访问参数