正在寻找有关我的C++;函数和数组的代码 对于我的C++项目,我已经在这个项目上挣扎了几个星期,我觉得我只是把自己和这个代码中的内容混淆了。有没有人能给我一些关于我做错了什么的指导?以下是我现在遇到的错误: 151 29[错误]无法将“(水果列表*)(&itemsList)”从“水果列表*”转换为“std::string{aka std::basic_string}” 152 35[错误]无法将“(水果列表*)(&itemsList)”从“水果列表*”转换为“std::string{aka std::basic_string}”
谢谢 我很抱歉,如果代码间距都弄乱了,这是我的第一篇文章,我不断地在我的代码间距上出错?提前道歉正在寻找有关我的C++;函数和数组的代码 对于我的C++项目,我已经在这个项目上挣扎了几个星期,我觉得我只是把自己和这个代码中的内容混淆了。有没有人能给我一些关于我做错了什么的指导?以下是我现在遇到的错误: 151 29[错误]无法将“(水果列表*)(&itemsList)”从“水果列表*”转换为“std::string{aka std::basic_string}” 152 35[错误]无法将“(水果列表*)(&itemsList)”从“水果列表*”转换为“std::string{aka std::basic_string}”,c++,arrays,C++,Arrays,谢谢 我很抱歉,如果代码间距都弄乱了,这是我的第一篇文章,我不断地在我的代码间距上出错?提前道歉 #include <iostream> //allows data to output to the screen #include <string> #include <vector> using namespace std; //function prototypes void showFruitList (
#include <iostream> //allows data to output to the screen
#include <string>
#include <vector>
using namespace std;
//function prototypes
void showFruitList (string);
void getData(string, int);
class Customer//create a class called customer
{
private://private section
//string all private variables
string firstName;
string lastName;
string streetAddress;
string city;
string state;
string zipCode;
public: //public sections
void setFName (string fName)//public function to set first name
{
firstName = fName;
}
string getFName ()//public function to get first name
{
return firstName;
}
void setLName (string lName)//public function to set last name
{
lastName = lName;
}
string getLName ()//public function to get last name
{
return lastName;
}
void setStreetAddress (string sAddress)//public function to set street ` address
{
streetAddress = sAddress;
}
string getStreetAddress()//public function to get street address
{
return streetAddress;
}
void setCity (string cty)//public function to set city
{
city = cty;
}
string getCity()//public function to get city
{
return city;
}
void setState (string sT)//public function to set state
{
state = sT;
}
string getState ()//public function to get state
{
return state;
}
void setZipCode (string zip)//public function to set zip code
{
zipCode = zip;
}
string getZipCode()//public function to get zip code
{
return zipCode;
}
};
struct FruitList //good for outputting lists of things to be called ` `in main function
{
string fruitItem; //string type
float fruitCostPP; //double type since prices for in decimal form ` `(fruit cost per pound)
};
//Establish customer registration and display a list of products
int main()
{
//declare variables
const int SIZE = 80;// assign value of 80 to SIZE
char firstName[ SIZE ];
Customer customer1 = Customer();//pulls from class Customer
string input;//welcome message and customer info input area
cout << "Welcome to Dried Fruit Central \n" <<endl;
cout << "Please enter your first name: "<< endl;//prompts user to enter ` `their first name
getline(cin, input);//user enters their first name
customer1.setFName(input);//pulls from class Customer
cout << "\nPlease enter your last name: " << endl; //promts user to ` ` `enter their last name
getline(cin, input);//user enters their last name
customer1.setLName(input);//pulls from class Customer
cout << "\nHello " << customer1.getFName() << "!"<< endl;// greeting to ` `user using their first name
cout << "\nPlease enter your street address: "<< endl;//prompts user for ` `their street address
getline(cin, input);//user enters their street address
customer1.setStreetAddress(input);//pulls from class Customer
cout << "\nPlease enter your city: "<< endl;//promts user to enter their ` `city
getline(cin, input);//user enters their city
customer1.setCity(input);//pulls from class Customer
cout << "\nPlease enter your state abbreviation: "<< endl;// prompts ` ` `user to enter their state abbreviation
getline(cin, input); //user enters their state abbreviation
customer1.setState(input);//pulls from class Customer
cout << "\nPlease enter your zip code: "<< endl;//promts user to enter ` `their zip code
getline(cin, input);//user enters their zipcode
customer1.setZipCode(input);//pulls from class Customer
cout << "\nThank you " << customer1.getFName() << ", you are now ` ` `registered and can begin shopping with \nDried Fruit Central! \n"<< ` ` endl;// lets customer know they are now registered and can begin ` ` shopping
cout << "\nHere is a list of our dried fruit available for purchase and ` `price per pound: \n"<< endl;//OUtput to customers the list of dried ` ` will be displayed as well as the price per pound
FruitList itemsList[8]; // 8 items on the menu
int itemNum = 0;//
char inputItemNum;//customers input fruit item number
vector<int> customerOrder;//customers order
while (true)//begin while loop
{
float totalCost=0;//double due to decimal amounts
getData(itemsList, 8); //pull from function getData
showFruitList(itemsList, 8); // pull function showFruitList
cout<<"Enter your orders. Press Enter after every input. Enter 0 to ` `end"<<endl;//customer inputs item numbers
do
{
while((cin>>itemNum).fail() || itemNum < 0 || itemNum > ` ` ` `9)//while loop to prevent customer from inputting the wrong item ` `number
{
cout<<"Enter correct item number : ";//error message
cin.clear();
cin.ignore();
}
customerOrder.push_back(itemNum - 1);
}while(itemNum != 0);
cout<<endl<<"You ordered"<<endl;//displays order total and items ` ` purchased
for(int i = 0; i < customerOrder.size() - 1; i++)
{
cout<<itemsList[customerOrder.at(i)].fruitItem<<"\t\t$ " ` ` ` `<<itemsList[customerOrder.at(i)].fruitCostPP<<endl;
totalCost += itemsList[customerOrder.at(i)].fruitCostPP;//all ` ` calculations
}
cout <<endl<<"Your order total is: $" << totalCost << endl << ` ` endl;//displays total cost
cout << "Input 'n' for a new order or 'Q' to quit \t";//allows users ` `to start new order or quit this order
cin >> inputItemNum;
if (inputItemNum == 'q' || inputItemNum == 'Q')
{
return 0;
}
else while (inputItemNum != 'n')//else while loop for error ` ` ` selection
{
cout << "Please make a valid selection.\t";//output error for ` ` valid selection to be entered
cin >> inputItemNum;
}
cout << endl << endl;
}
// system("pause");
return 0;
}
void showFruitList(FruitList list[], int size) //calls in structure ` `of fruit menu and price together to be displayed when called in main
{
cout << "Type the number of the items that you would like.""\n";
cout << "After each selection, press Enter." "\n";
cout << "To add multiple orders of the same fruit enter the same item ` `number." "\n\n\n" << endl;
cout << "When you are done, type 0 and press Enter to get your order ` `total." "\n\n\n" << endl;
for (int i = 0; i < size; i++) //goes with size of list 8, and adds a ` `.) and $ in front of each item
{
cout << "\t\t" << i + 1 << ") " << list[i].fruitItem << "$ " << ` ` list[i].fruitCostPP << endl; //structure. i+1 to start num at 1 ` ` rather than 0
cout << "\n";
}
return;
}
void getData(FruitList list[], int size) //two arrays that list the ` `item and one for the price 0-8
{
list[0].fruitItem = "Apricots.\t\t"; //items array starts at 0. ` ` output at 1.
list[0].fruitCostPP = 1.75;//price array starts at 0
list[1].fruitItem = "Apples.\t\t";
list[1].fruitCostPP = 1.25;
list[2].fruitItem = "Bananas.\t\t";
list[2].fruitCostPP = 1.25;
list[3].fruitItem = "Cherries.\t\t";
list[3].fruitCostPP = 2.25;
list[4].fruitItem = "Cranberries.\t\t";
list[4].fruitCostPP = 1.25;
list[5].fruitItem = "Mangos.\t\t";
list[5].fruitCostPP = 1.75;
list[6].fruitItem = "Peaches.\t\t";
list[6].fruitCostPP = 1.25;
list[7].fruitItem = "Strawberries.\t";
list[7].fruitCostPP = 1.75;
}
#include//允许数据输出到屏幕
#包括
#包括
使用名称空间std;
//功能原型
无效显示列表(字符串);
void getData(字符串,int);
类Customer//创建一个名为Customer的类
{
private://private 部分
//字符串所有私有变量
字符串名;
字符串lastName;
字符串地址;
字符串城市;
字符串状态;
字符串zipCode;
公众://公众部分
void setFName(string fName)//用于设置名字的公共函数
{
firstName=fName;
}
string getFName()//获取名字的公共函数
{
返回名字;
}
void setLName(string lName)//用于设置姓氏的公共函数
{
lastName=lName;
}
string getLName()//获取姓氏的公共函数
{
返回姓氏;
}
void setStreetAddress(字符串sAddress)//用于设置street`地址的公共函数
{
streetAddress=sAddress;
}
string getStreetAddress()//获取街道地址的公共函数
{
返回街道地址;
}
void setCity(字符串cty)//用于设置城市的公共函数
{
城市=cty;
}
string getCity()//获取城市的公共函数
{
回归城市;
}
void setState(string sT)//用于设置状态的公共函数
{
state=sT;
}
string getState()//获取状态的公共函数
{
返回状态;
}
void setZipCode(string zip)//用于设置邮政编码的公共函数
{
zipCode=zip;
}
string getZipCode()//获取邮政编码的公共函数
{
返回zipCode;
}
};
struct FruitList//用于输出要在main函数中调用的东西的列表
{
字符串项;//字符串类型
浮动水果成本PP;//由于十进制形式的价格为```(每磅水果成本),因此为双倍类型
};
//建立客户注册并显示产品列表
int main()
{
//声明变量
const int SIZE=80;//为SIZE赋值80
字符名[大小];
Customer customer1=Customer();//从类Customer中提取
字符串输入;//欢迎信息和客户信息输入区
cout它与功能原型无关,而是与结构定义有关
如果你想实际使用一个结构,它必须被完整地定义。在你调用函数之后放置函数定义(实现)是可以的,但是对于结构来说这是不可能的
要声明结构的实际变量,编译器需要结构的完整定义,否则编译器将不知道它包含哪些成员或它有多大
因此,为了解决您的问题,不要在顶部有一个简单的结构前向声明,请将整个结构定义放在那里。您可以前向声明一个类型(例如结果列表
)这样编译器就知道它存在,但在你告诉编译器它是如何定义的之前,你不能实际使用它的任何成员。因此你的struct-foultlist
定义需要在引用结构的任何成员之前出现。谢谢你的帮助!我删除了结构的原型并将结构函数移回但是,当然我现在有两个新错误:151 29[error]无法将(水果列表*)(&itemsList)从“水果列表*”转换为“std::string{aka std::basic_string}”152 35[error]无法将(水果列表*)(&itemsList)'从“水果列表*”转换为“std::string{aka std::basic_string}”“我正试图浏览我的书,寻找关于我所做的事情的信息,我已经在这门课上挣扎了几周,现在我很困惑。@KatherinePhipps是showFruitList
函数,它以FruitList
指针作为参数?它应该这样做吗?如果你有一个像itemsList
这样的数组,那么就使用e其地址(即&itemsList
)在大多数情况下都不会像您期望的那样工作。数组自然会衰减为指向其第一个元素的指针,如果您有一个函数需要指向某个结构的指针,只需将数组作为参数传递给该函数就足够了。在修复了其中的所有错误后,我需要做的下一件事是提供指针的使用,而不需要wor关于分配动态内存的问题。我只需要演示如何理解指针在C++语言中的作用。我仍然在学习指针,现在强调我将如何添加它。在哪里,你是说我需要一个来工作,或者现在我不能正确地放置一个指针。