using namespace std;和矢量

时间:2015-07-16 19:39:21

标签: c++ string vector

我正在为我的c ++类开发一个项目,虽然我的代码看起来很好(初学者)每当我运行它时,我几乎每行都会得到错误C2065未声明的标识符。我已经完成了我的研究并且感觉它与使用namespace std有关;但我不确定。任何帮助将不胜感激。

该项目是创建一个存储书籍列表的向量,然后为用户提供将书籍添加到列表,使用迭代器显示列表,使用迭代器删除书籍或将数据存储在列表中的功能。外部文件。以下是我到目前为止的情况:

#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;
}

2 个答案:

答案 0 :(得分:2)

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::

答案 1 :(得分:1)

在头文件中避免using namespace std;,就像瘟疫一样。 Read here for why

在Nathan编辑之后,很明显string对于你的原型来说还不够。编译器还不知道该类型是什么,因为std:stringstring之后不能简单地using namespace std;可用。解决方案是不要将标题包括在using namespace std下面;相反,您首先不会依赖using namespace std;或.cpp文件来获取包含头的依赖项。换句话说,那个标题开头是错误的。看起来应该是这样的:

#ifndef MYAPP_BOOKCOLL_H
#define MYAPP_BOOKCOLL_H

#include <string>

std::string getBook();

#endif

关于遗漏#pragma once的辩论,我拒绝参与其中。从那里,您的.cpp文件可能如下所示:

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

std::string getBook()
{
    std::string newBook;
    std::cout << "What is the name of the book you would like to add?" << std::endl;
    std::getline(std::cin, newBook);
    return newBook;
}

包含bookColl.h作为第一个标头可确保您的用户定义的头文件bookColl.h得到正确实现,并且不依赖于.cpp文件,包括标头本身所依赖的内容(因此应该包括自己)。它不能(也不应该)依赖于消费.cpp文件之前的系统头包含声明。

我留下如何在main.cpp文件中包含bookColl.h以及该文件中的错误,供您解决。