我的函數output_all,add_item和remove_item都是從main.cc文件中得到這個錯誤。我錯過了什麼?我認爲這很簡單,但很明顯我搞砸了。我將在下面包含我的文件。預先感謝您的任何幫助!函數未在此範圍內聲明。我錯過了什麼?
main.cc
#include <iostream>
#include <string>
#include <cstdlib>
#include "basket.h"
using namespace std;
int main(){
int choice;
string item;
cout << "Would you like to: \n\n 1.See List \n 2.Add an Item \n 3.Remove an item \n 4.Exit \n\n Please Enter a number: ";
cin >> choice;
if (choice == 1){
output_all(cout);
}
else if (choice == 2){
cout << "Please enter the item you would like to add to the list: ";
cin >> item;
add_item(item);
}
else if (choice == 3){
cout << "Please enter the name of the item you would like to remove: ";
cin >> item;
remove_item(item);
}
else if (choice == 4){
exit(1);
}
else {
main();
}
return (0);
}
basket.h
#include <iostream>
#include <string>
using namespace std;
class basket{
public:
//function will output all of the items currently in the list
void output_all(ostream& fout);
//Function will allow user to add item to list
void add_item(const string& item);
//function will remove item from list after checking that item exists in the list
void remove_item(const string& item);
// accessor method to access the string name
string get_name();
private:
//string variable used for storing thr list
string name;
};
basket.cc
#include <iostream>
#include <string>
#include "basket.h"
using namespace std;
void basket::output_all(ostream& fout){
fout << name;
}
void basket::add_item(const string& item){
name = name + " " + item;
}
void basket::remove_item(const string& item){
int num = name.find(item);
name.erase(num, item.length());
}
string basket::get_name(){
return (name);
}
您的函數是類中的方法(並且不是靜態的),所以您需要創建該類的實例並調用方法。他們不是免費的功能。 – 2014-12-02 18:46:16