返回字符串向量

Rya*_*yan 0 c++ string vector

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

main.cpp中

#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);
Run Code Online (Sandbox Code Playgroud)

lab1.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
Run Code Online (Sandbox Code Playgroud)

lab1.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;
}
Run Code Online (Sandbox Code Playgroud)

这是编译时的错误.

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
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此错误以及如何解决此问题.感谢您提供任何帮助.

瑞安

Luc*_*ore 5

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

事情是你的包含顺序是:

#include "lab1.h"
#include <vector>
Run Code Online (Sandbox Code Playgroud)

因为你std::vector在标题中使用(包括它之前),你会得到错误.反转包含顺序将修复编译错误,但不解决基础错误 - lab1使用尚未定义的符号.正确的解决方案是包括<vector>.