c++ - 函数不接受 1 个参数错误

标签 c++ vector arguments enumerate

我已经尝试了很多次来修复这个错误,但我不确定该怎么做。对于 addBooks 和 displayBooks 函数,我收到“函数不接受 1 个参数”错误,尽管 vector 应该只是一个参数。

struct bookStruct
{
    char title[40];
    char author[40];
    int pages;
    int year;
};

enum menu { display=1, add, end} ;

void displayOptions();
void displayBooks();
void addBooks();

int main(){

    vector<bookStruct> book(1);
    string option = "display";

    displayOptions();
    cin >> option;

    //std::strcpy(book[0].title, "a");
    //std::strcpy(book[0].author, "a");
    //book[0].pages = 0;
    //book[0].year = 0;

    while (option != "end"){
        addBooks(book);
        displayBooks(book);
    }

    return 0;
}

void displayOptions(){

    cout << "1. Display list of books" << endl;
    cout << "2. Add books" << endl;
    cout << "3. Exit" << endl;

}

void displayBooks(vector<bookStruct> book){
    for (int n = 0; n<book.size(); n++){
        cout << book[n].title << " ; " <<  book[n].author << " ; " 
            << book[n].pages << " ; " << book[n].year <<endl;

    }

    cout << endl;
}

void addBooks(vector<bookStruct> book){
    int n = book.size()+1;
    book.resize(book.size()+1);
    cout << "Enter the book title: " << endl;
    cin >> book[n].title;
    cout << "Enter the author name: " << endl;
    cin >> book[n].author;
    cout << "Enter the number of pages: " << endl;
    cin >> book[n].pages;
    cout << "Enter the publication year: " << endl;
    cin >> book[n].year;
}

最佳答案

addBooksdisplayBooks 都不带参数:

void displayBooks();
void addBooks();

然而你却在用参数调用它们:

addBooks(book);
displayBooks(book);

编译器用自己的话告诉你这一点。

看来你需要

void displayBooks(vector<bookStruct> book);
void addBooks(vector<bookStruct> book);

尽管您更有可能不需要将 vector 复制到函数中:

void displayBooks(const vector<bookStruct>& book);
void addBooks(const vector<bookStruct>& book);

注意 main() 后有单参数函数的定义。 main() 函数只考虑它之前的声明。

关于c++ - 函数不接受 1 个参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15809610/

相关文章:

c++ - GL/glx 没有正确链接

c++ - 在C++中创建线程时出错

c++ - 在 AIX 中启动 C++ 可执行文件时出错

c++ - 如何在给定指向该元素的指针的情况下找到排序 vector 中元素的索引

python - python 函数中默认参数的值

python - 有什么方法可以知道参数的值是默认值还是用户指定的?

c++ - 有没有办法从基类函数参数中推导出模板参数?

c++ - "listening"到 C/C++ 中的文件更改(在 Windows 上)

c# - 在 C# ILNumerics Vector 上运行

C++:字符串流到 vector