对于我在C ++中的项目,我已经在这个项目中苦苦挣扎了几个星期了,而且我觉得我只是把自己与我添加到这段代码中的内容混为一谈。谁能给我一些关于我做错的指导?以下是我现在遇到的错误: 151 29 [错误]无法将'(FruitList *)(& itemsList)'从'FruitList *'转换为'std :: string {aka std :: basic_string}' 152 35 [错误]无法将'(FruitList *)(& itemsList)'从'FruitList *'转换为'std :: string {aka std :: basic_string}'
谢谢!
我很抱歉,如果代码间距全部搞砸了,这是我的第一篇帖子,我的代码间隔方面一直出错?提前抱歉!
#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;
}
答案 0 :(得分:0)
它与 function 原型无关,但与结构定义无关。
如果您想实际使用结构,则必须完整定义。在调用函数之后放置函数定义(实现)是可以的,但是对于不可能的结构。
要声明结构的实际变量,编译器需要完整的结构定义,否则编译器不会知道它包含的成员数或者它有多大。
因此,要解决您的问题,请不要在顶部对结构进行简单的前向声明,将整个结构定义放在那里。