0

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);

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

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

これがコンパイルのエラーです。

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」に。ただし、「lab1.cpp」を移動して、「lab1.h」の読み取りを開始する前に「`の前#include <vector>に来るようにすることで、問題を隠すことができます。#include "lab1.h". That would make the error go away, because the compiler would have already readそれはあなたがすべきことではありませんが、それは偶然に起こり、実際の問題を隠すようなものです。

于 2012-09-03T18:58:18.517 に答える