c++ - 返回字符串 vector

标签 c++ string vector

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

主要.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);

实验室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

为什么会出现此错误以及如何修复它。感谢您提供的任何帮助。

瑞安

最佳答案

你必须 #include <vector>在头文件中也是如此。实际上,将它包含在 header 中就足够了,因为包含该 header 的所有文件都将隐式包含 <vector> .

问题是您的包含顺序是:

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

并且由于您使用 std::vector在 header 中(在包含它之前)您会收到错误消息。反转包含顺序会修复编译错误,但不会解决潜在的错误 - lab1使用尚 undefined symbol 。正确的解决方法是包括 <vector> .

关于c++ - 返回字符串 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12252795/

相关文章:

java - 如何将 vector 中的元素放入ArrayList中

c++ - 在类模板实例化中为某些参数显式使用默认值

c++ - 使用 C++ OOP 设计更新总值

c# - 如何检查 list<string> 是否包含任何字符串值

java - 从格式化字符串中读取值 Java、Groovy

java - 将字符串转换为 java.util.date

c++ - 从 vector 改变事物

c++ - STL 中的 vector push_back?

c++ - 用指向实例中方法的指针替换参数中的静态函数指针

c++ - 括号分析的递归函数