0

我对向量的所有命名空间以及如何在我的类中正确返回字符串向量感到有点困惑。这是代码:

主文件

#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include "lab1.h"

using namespace std;
readwords wordsinfile;
words wordslist;

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) {
            // Looks like we have no arguments and need do something about it
            // Lets tell the user
            cout << "Usage: " << argv[0] <<" <filename>\n";
            exit(1);
    } else {
            // Yeah we have arguements so lets make sure the file exists and it is readable
            ifstream ourfile(argv[1]);
            if (!ourfile.is_open()) {
                    // Then we have a problem opening the file
                    // Lets tell the user and exit
                    cout << "Error: " << argv[0] << " could not open the file. Exiting\n";
                    exit (1);
            }

            // Do we have a ASCII file?
            if (isasciifile(ourfile)) {
                    cout << "Error: " << argv[0] << " only can handle ASCII or non empty files. Exiting\n";
                    exit(1);
            }

            // Let ensure we are at the start of the file
            ourfile.seekg (0, ios::beg);
            // Now lets close it up
            ourfile.close();
    }

    // Ok looks like we have past our tests
    // Time to go to work on the file
    ifstream ourfile2(argv[1]);
    wordsinfile.getwords(ourfile2);

实验室1.h

#ifndef LAB1_H
#define LAB1_H

bool isasciifile(std::istream& file);

class readwords {
    public:
             int countwords(std::istream& file);
             std::vector<std::string> getwords(std::istream& file);
};

class words {
    public:
            void countall( void );
            void print( void );
};

#endif

实验室1.cpp

#include <fstream>
#include <iostream>
#include <map>
#include "lab1.h"
#include <vector>
using std::vector;
#include <string>

using namespace std;

vector<string> readwords::getwords(std::istream& file) {
    char c;
    string aword;
    vector<string> sv;
    int i = 0;

                    while(file.good()) {
                            c = file.get();
                            if (isalnum(c)) {
                                    if(isupper(c)) {
                                            c = (tolower(c));
                                    }
                                    if(isspace(c)) { continue; }
                                    aword.insert(aword.end(),c);
                            } else {
                                    if (aword != "") {sv.push_back(aword);}
                                    aword = "";
                                    i++;
                                    continue;
                            }
                    }
    return sv;
}

这是编译的错误。

g++ -g -o lab1 -Wall -pedantic main.cpp lab1.cpp
In file included from lab1.cpp:4:0:
lab1.h:9:4: error: ‘vector’ in namespace ‘std’ does not name a type
lab1.cpp:48:54: error: no ‘std::vector<std::basic_string<char> > readwords::getwords(std::istream&)’ member function declared in class ‘readwords’
make: *** [lab1] Error 1

为什么我会收到此错误以及如何修复它。感谢您提供任何帮助。

瑞安

4

2 回答 2

5

您还必须#include <vector>在头文件中。实际上,将其包含在标头中就足够了,因为包含该标头的所有文件都将隐式包含<vector>.

问题是您的包含订单是:

#include "lab1.h"
#include <vector>

并且由于您std::vector在标题中使用(在包含它之前),您会收到错误消息。反转包含顺序将修复编译错误,但不能解决底层错误 -lab1使用尚未定义的符号。正确的解决方法是包含<vector>.

于 2012-09-03T18:55:05.210 回答
1

编译器按照编写的顺序查看代码。这也适用于#include 指令:文件的内容被视为已写入#include 的文件中。正如@LuchianGrigore 所提到的,最好的解决方案是添加

#include <vector>

到“lab1.h”。但是你可以通过移动#include <vector>“lab1.cpp”来隐藏问题,以便它在#include "lab1.h". That would make the error go away, because the compiler would have already read开始读取“lab1.h”之前出现在`之前。这不是你应该做的,但它可能会意外发生并隐藏实际问题。

于 2012-09-03T18:58:18.517 回答