2015-10-25 3 views
2

Я получил это супер простой код, который я пытаюсь скомпилировать под Linux (первый раз работаю под Linux) Я запрограммированный C++ раньше, и я понимаю концепцию заголовков и CPP файлов ...заголовков и CPP файлы

Sentence.h

#ifndef SENTENCE_H_ 
#define SENTENCE_H_ 

std::vector<std::string> split(char sentence[]); 
void printWords(std::vector<std::string> words); 

#endif 

Sentence.cpp

#include "../include/Sentence.h" 
#include <iostream> 
#include <sstream> 
#include <string> 
#include <vector> 

std::vector<std::string> split(char sentence[]) { 
    string str(sentence); 
    string buf; // Have a buffer string 
    stringstream ss(str); // Insert the string into a stream 

    vector<string> tokens; // Create vector to hold our words 

    while (ss >> buf) 
     tokens.push_back(buf); 

    return tokens; 
} 

void printWords(std::vector<std::string> words) { 
    for (size_t i = 0; i < words.size(); ++i) 
      std::cout << words[i] << endl; 
} 

И я получаю те несколько ошибок, которые я не могу понять, почему ...

[email protected]:~/workspace/HW1$ make 
g++ -g -Wall -Weffc++ -c -Linclude -o bin/Sentence.o src/Sentence.cpp 
In file included from src/Sentence.cpp:1:0: 
src/../include/Sentence.h:4:6: error: ‘vector’ in namespace ‘std’ does not name a template type 
std::vector<std::string> split(char sentence[]); 
    ^
src/../include/Sentence.h:5:22: error: variable or field ‘printWords’ declared void 
void printWords(std::vector<std::string> words); 
        ^
src/../include/Sentence.h:5:17: error: ‘vector’ is not a member of ‘std’ 
void printWords(std::vector<std::string> words); 
       ^
src/../include/Sentence.h:5:29: error: ‘string’ is not a member of ‘std’ 
void printWords(std::vector<std::string> words); 
          ^
src/../include/Sentence.h:5:42: error: ‘words’ was not declared in this scope 
void printWords(std::vector<std::string> words); 
             ^
makefile:10: recipe for target 'bin/Sentence.o' failed 
make: *** [bin/Sentence.o] Error 1 
+4

Подумайте о порядке ваших директив '# include'. –

+0

Требуется ли «printWords» скопировать вектор только для его вывода? –

ответ

2

Sentence.h использует std::vector, но не включает <vector> заголовок. Такая же ситуация с std::string.

4

Ваш заголовок использует класс std :: vector, но код класса вектора никогда не включается в него. Добавьте #include <vector> в заголовочный файл, чтобы класс загружался до остальной части Sentence.h.

Это также поможет в других файлах, которые могут использовать Sentece.h, так как им не нужно будет включать векторный класс самостоятельно.

Смежные вопросы