标头和 CPP 文件
headers and cpp files
我得到了这个超级简单的代码,我正在尝试在Linux下编译(第一次在linux下工作(我以前用 c++ 编程,我理解头和 cpp 文件的概念......
句子.h
#ifndef SENTENCE_H_
#define SENTENCE_H_
std::vector<std::string> split(char sentence[]);
void printWords(std::vector<std::string> words);
#endif
句子.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;
}
而且我遇到了那几个错误,我不知道为什么......
daniel@daniel-ubuntu-server:~/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
你的标头使用 std::vector 类,但向量类代码从未包含在其中。将#include <vector>
添加到头文件,以便在Sentence.h
的其余部分之前加载类。
这也将有助于其他可能使用 Sentece.h
的文件,因为它们不必自己包含向量类。
Sentence.h
使用std::vector
但不包括<vector>
标头。与std::string
的情况相同.
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 为什么我的主文件.cpp不打印头文件中的任何内容
- 生成文件错误 - 找不到文件 - *.cpp
- 如何在文件.cpp gtkmm中声明小部件
- 没有实现文件(.cpp)的派生类
- 如何使用"CMakeLists.txt"中的add_library将整个文件(.cpp,.h等)包含在目录中
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 关于 Linux .so 文件无法链接到主文件.cpp文件
- 文件.cpp从Windows到Linux
- Ubuntu C++ 编译器错误: g++: 错误: 文件.cpp: 没有这样的文件或目录
- 如何将文件.cpp编译为本机编译
- 使用模板类时,似乎无法包含除 main 以外的任何 cpp 文件.cpp
- 无法编译C++文件.cpp。C++98模式
- 为什么C++头文件不需要包含实现文件 (.cpp)
- 如何访问位于独立文件(.cpp)中的非成员函数
- 如何在Visual Studio Code中通过键盘快捷键切换头文件/cpp文件
- 如何将数据从stringstream写入文件(CPP)
- 为什么filestream不把空白写入文件cpp ?