gpt4 book ai didi

c++ - 使用命名空间标准;和载体

转载 作者:行者123 更新时间:2023-11-28 00:14:03 25 4
gpt4 key购买 nike

我正在为我的 c++ 类开发一个项目,虽然我的代码对我(初学者)来说看起来不错,但每当我运行它时,我几乎每一行都会收到错误 C2065 未声明的标识符。我做了研究,感觉它与使用命名空间标准有关;但我不确定。任何帮助将不胜感激。

该项目是创建一个存储书籍列表的 vector ,然后为用户提供向列表中添加书籍、使用迭代器显示列表、使用迭代器删除书籍或将数据存储在外部文件。这是我目前所拥有的:

#include <iostream>
#include <vector>
#include <string>
#include "bookColl.h"
#include <algorithm>
#include <fstream>

using namespace std;

int main()
{
vector <string> bookCollection;
string book1, book2, book3;

cout << "Enter the first book in your book collection: " << endl;
getline(cin, book1);

cout << "\nEnter the second book in your book collection: " << endl;
getline(cin, book2);

cout << "\nEnter the third book in your book collection: " << endl;
getline(cin, book3);

bookCollection.push_back(book1);
bookCollection.push_back(book2);
bookCollection.push_back(book3);

vector <string> ::iterator myITER;
vector <string> ::const_iterator cITER;

double decide;

do
{
int choice = 0;
cout << "What would you like to do?/n1.Add a book to your collection." <<
"/n2.Display your book collection." << "/n3.Remove a book from your collection" << "/n4. Stop adding books and store the data in an external file" << "/nChoose a number 1 through 4: " << endl;

if (choice == 1)
{
string addNew;
addBook(&addNew);
bookCollection.push_back(addNew);
}

else if (choice == 2)
{
cout << "Here are your books: " << endl;
for (cITER = bookCollection.begin(); cITER != bookCollection.end(); cITER++)
{
cout << *cITER << endl;
}
}

else if (choice == 3)
{
int removeBook;
myITER = bookCollection.erase(bookCollection.begin() + removeBook);
cout << "enter the numbered book position for the book you would like to remove: " <<
"/n(The books in your book collection are labels for 0 up)" << endl;
cin >> removeBook;
cout << *myITER << endl;
}

else if (choice == 4)
{
sort(bookCollection.begin(), bookCollection.end());

ofstream bookFile;
bookFile.open("BookCollection.txt", ios::out | ios::app);
bookFile << " This is a line of text. \n";
bookFile << " This is a second line of text. \n";
bookFile.close();
}

cout << "\nWould you like to run this program again? If so select 1 for Yes, and 0 for No" << endl;
cin >> decide;

} while (decide > 0);
}

bookColl.h

string addBook(string* pValOne);

bookColl.cpp

#include <iostream>
#include <vector>
#include <string>
#include "bookColl.h"

using namespace std;

string addBook(string* pValOne)
{
string newBook;
*pValOne = newBook;
cout << "What is the name of the book you would like to add?" << endl;
getline(cin, *pValOne);
return newBook;
}

最佳答案

bookColl.h 必须如下所示:

#ifndef BOOK_COLL_H
#define BOOK_COLL_H

#include <string>

std::string addBook(std::string* pValOne);

#endif

你需要一个 include guard ,类的全称是std::string , 不是 string ;这就是你用 #include <string> 得到的.您的代码无法编译,因为 using namespace std; #include "bookColl.h" 之后.

说到 using namespace ,我的建议是忘记它,至少现在是这样。只需使用 std::无处不在。

关于c++ - 使用命名空间标准;和载体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31463092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com