简体   繁体   English

std lib对象作为返回类型和函数参数

[英]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.

相关问题 std::size_t 与 size_type 作为参数和 function 返回类型 - std::size_t vs. size_type as parameters and function return types 参数包中的参数计数? 这有一个C ++ 0x std lib函数吗? - Count of parameters in a parameter pack? Is there a C++0x std lib function for this? 不完整类型作为函数参数和返回值 - Incomplete types as function parameters and return values 与 std::function 类似,但具有更多不同的参数和返回类型 - Like std::function but with more varied argument and return types ADL with std :: function:可以通过std :: function的参数列表中的类型找到带std :: function对象的函数吗? - ADL with std::function: Can functions taking std::function objects be found via the types in the std::function's argument list? std :: function参数类型 - std::function argument types 如何比较参数包中的类型(包括引用限定符)和 std::function 参数的类型 - How to compare types (including ref-qualifiers) in a parameter pack and the types of std::function parameters 将特征类型作为参数的函数的返回类型不清楚 - Unclear return type for function taking eigen types as parameters std :: function复制参数? - std::function copying parameters? 如何从 RcppArmadillo 函数返回不同类型的对象 - How to return objects of different types from RcppArmadillo function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM