没有运算符<<将这些操作数与 WITH 匹配 #include<string>
No operator << matches these operands WITH #include<string>
我已经有同样的错误两天了:到目前为止我的代码的最佳版本如下,编译器一直抱怨"没有操作符<<"匹配这些操作数",尽管我按照另一个主题的建议做了#include。我也不知道我是否应该把所有的标题和东西都放在我的帖子里,因为那样很拥挤。
整个程序并不是真的那么相关,我试图创建一个纯虚函数"vector tostand (int)"(所以它应该返回一个向量,并以int作为参数)。由于某些原因,它从来没有工作过,这就是为什么我使用了另一个程序,我确信它可以工作,我完全剥离了它。不过,到目前为止还没有运气。
我标记了最重要的部分,这些部分表明c++在哪些方面与我不一致
基类头:
#ifndef BasisToestand_H
#define BasisToestand_H
#include<vector>
using std::vector;
#include <string>
using std::string;
class BasisToestand
{
public:
BasisToestand (const string="geen naam");
virtual ~BasisToestand();
//void setName( const string );
// Get Functions are all declared const
const string getName() const;
virtual const double getVal(double) const = 0; //THIS WORKS
//virtual const vector<double> Toestand(int) const = 0; //THIS DOES NOT
// Overloaded operators is also declared const and virtual
virtual const double operator()(double = 0.) const = 0; //THIS WORKS
private:
string T_Naam;
};
#endif
基类cpp:
#include <iostream>
using std::cout;
using std::endl;
#include<vector>
using std::vector;
#include <string>
using std::string;
#include <cstdlib>
using std::exit;
#include "BasisToestand.h"
BasisToestand::BasisToestand (const string nieuwe_naam)
: T_Naam(nieuwe_naam)
{
cout << "calling base class BasisToestand constructor for " << T_Naam << endl;
}
BasisToestand::~BasisToestand()
{
cout << "calling base class BasisToestand destructor for " << T_Naam << endl;
}
const string BasisToestand::getName () const
{
return T_Naam;
}
派生类头文件:
#ifndef T_Tsunami_H // preprocessor wrapper
#define T_Tsunami_H
#include <string>
using std::string;
#include "BasisToestand.h" // base class header
const static double PI = 3.1415926535897932384626433832795;
class T_Tsunami : public BasisToestand
{
public:
// Constructor with default arguments
T_Tsunami (const double norm = 1., const double mean = 0.,
const double sigma = 1., const string="T_Tsunami");
~T_Tsunami(); // destructor
// Set Functions
void setNorm( const double norm = 1. );
void setMean( const double mean = 0. );
void setSigma( const double sigma = 1. );
// Get Functions are all declared const
const double getNorm() const;
const double getMean() const;
const double getSigma() const;
virtual const double getVal(double) const; //THIS WORKS
//virtual const vector<double> Toestand(int) const; //PROBLEM
// Overloaded operators is also declared const
virtual const double operator()(double = 0.) const; //THIS WORKS
private:
double p0;
double p1;
double p2;
};
派生类。cpp
#include <iostream>
using std::cout;
using std::endl;
#include <cmath>
#include "T_Tsunami.h" // Only T_Tsunami header file needed
T_Tsunami::T_Tsunami (const double norm, const double mean,
const double sigma, const string nieuwe_naam)
: BasisToestand(nieuwe_naam),
p0(norm),
p1(mean),
p2(sigma)
{
cout << "calling derived class T_Tsunami constructor for " << getName() << endl;
}
T_Tsunami::~T_Tsunami()
{
cout << "calling derived class T_Tsunami destructor for " << getName() << endl;
}
const double T_Tsunami::getVal(double x) const
{
return p0/p2/(sqrt(2*PI))*exp(-pow((x-p1),2)/(2*pow(p2,2)));
}
const double T_Tsunami::operator()(double x) const // overloaded () operator WORKS
{
return getVal(x);
}
void T_Tsunami::setNorm (const double norm)
{
p0 = norm;
}
void T_Tsunami::setMean (const double mean)
{
p1 = mean;
}
void T_Tsunami::setSigma (const double sigma)
{
p2 = sigma;
}
const double T_Tsunami::getNorm() const
{
return p0;
}
const double T_Tsunami::getMean() const
{
return p1;
}
const double T_Tsunami::getSigma() const
{
return p2;
}
//THIS IS WHAT MY VIRTUAL FUNCTION "TOESTAND" SHOULD DO FOR THIS DERIVED CLASS
const vector<double> BasisToestand::Toestand(int GOLF) const
{
vector<double>T_1;
for( int i = 0; i < GOLF; i++ )
{ double y = 0.25*(1-tanh(double(i-75)/5));
T_1.push_back(y);
}
cout<< "Creating vector T_1" << endl;
return T_1;
}
主要功能:
#include <iostream>
using std::cout;
using std::endl;
using std::scientific;
#include <string>
using std::string;
#include <cmath>
#include <iomanip>
using std::setw;
using std::setprecision;
#include <vector>
using std::vector;
#include "BasisToestand.h"
#include "T_Tsunami.h"
int main()
{
T_Tsunami myTsunami_1;
BasisToestand *funPtr1 = &myTsunami_1;
BasisToestand& funRef1 = myTsunami_1;
cout << "value at x=0 using pointers is " << funPtr1->getVal(0.) << endl; //WORKS
cout << "value at x=0 using references is " << funRef1(0.) << endl; //WORKS
cout << "Testing Tsunami" **<<** funPtr1->Toestand(10) << endl;
//THIS DOES NOT WORK, the bold thing is where I get the error.
return 0;
}
你的问题可以简化为:
#include <vector>
#include <iostream>
int main() {
std::vector<int> v;
std::cout << v;
}
main.cpp:5:19: error: no match for 'operator<<'(操作数类型为'std::ostream{又称std::basic_ostream}'和'std::vector')
这个错误很好地解释了问题。您不能简单地使用operator <<
将容器发送到流。你可以使用别人编写的功能(c++ STL容器的漂亮打印),或者简单地遍历vector的内容并自己完成。
cout << "Testing Tsunami ";
const vector<double>& Toestand = funPtr1->Toestand(10); //get a reference to the vector
for(int i=0; i<Toestand.size(); ++i) //for each element
cout << Toestand[i] << ','; //print the element and a comma
cout << endl;
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 基于ELO的团队匹配算法
- 没有用于初始化C++中的变量模板的匹配构造函数
- 呼叫运营商<<临时
- 调用'begin(int [n])'没有匹配函数
- 将模板与类模板扣除占位符参数匹配
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- <<操作员在下面的行中工作
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 在使用累加时,C++中的运算符+不匹配
- 错误:调用'getline'没有匹配函数
- 没有匹配的“运营商&lt;&lt;”
- C++ostream:没有运算符匹配<<&应在'&'代币
- 错误:没有匹配'运算符<<"在'std::cout