[英]std lib objects as return types and function parameters
I am new to C++ and the idea of header files defining classes is foreign to me. 我是C ++的新手,定义类的头文件的想法对我来说很陌生。 I have the following header file for a Polynomial class that is giving me a lot of errors.
我有一个Polynomial类的以下头文件,它给了我很多错误。
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial {
public:
Polynomial(std::vector <int>&);
Polynomial(const Polynomial& orig);
virtual ~Polynomial();
std::vector <int> getCoeffs();
Polynomial getIntegral(int, int, int);
Polynomial getDerivative(int);
std::string toString();
void integrate(int, int);
void derive();
private:
std::vector<int> coeffs;
};
#endif /* POLYNOMIAL_H */
All of the class methods that include something from the standard lib in their return type definition give me the error: 'vector' (or 'string') in namespace 'std' does not name a type 所有在返回类型定义中包含标准库中某些内容的类方法都会给出错误:命名空间'std'中的'vector'(或'string')没有命名类型
Also the constructor which takes a vector as a parameter gives the error: expected ')' before '<' token. 将vector作为参数的构造函数在'<'标记之前给出错误:expected')'。
I'm sure this is something very obvious, but whatever it is the tutorials I have done haven't gone this deep into classes and class definitions to come across an example like this. 我确信这是非常明显的,但不管是什么教程我都没有深入到类和类定义中来遇到像这样的例子。
std::vector
is defined in the header vector . std::vector
在头部向量中定义。 You need to add the statement 您需要添加语句
#include <vector>
at the top of your header file. 在头文件的顶部。 This causes the preprocessor to (effectively) paste the contents of that file in place of the
#include
statement. 这会导致预处理器(有效地)粘贴该文件的内容而不是
#include
语句。 Thus the compiler knows what the type std::vector
refers to in your class definition. 因此编译器知道类定义中
std::vector
引用的类型。
The same applies to std::string
, which is, in turn, defined in the header string . 这同样适用于
std::string
,而std::string
又在头字符串中定义。 So add #include <string>
for that header. 因此,为该标头添加
#include <string>
。
cppreference.com is a good reference to search for types and their respective headers; cppreference.com是搜索类型及其各自标题的好参考; it is also a good online reference in general for C++.
对于C ++来说,它也是一个很好的在线参考。
This is what your header file should look like: 这是您的头文件应该是这样的:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <string>
#include <vector>
class Polynomial {
...
};
#endif /* POLYNOMIAL_H */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.