ERROR: no match for 'operator in 'std::cout
Why the error?
The compiler reports an error because there is no overloaded version of <<
operator to handle the type vector<triangle>
which your function findRightTriangles()
returns.<<
is overloaded only for most of the built-in data types and not for custom classes.
How to output the vector using cout?
There are two ways:
Solution 1:
A two step procedure:
Step1: You will have to iterate through the vector and cout
each contained triangle
.
std::vector<triangle>::const_iterator iter= vec.begin();
for(iter; iter != vec.end(); ++iter)
{
cout<<*iter; //This is what step 2 provides for
}
Step 2: You will have to overload <<
for the triangle
class as well.
ostream& operator<<( ostream& os, const triangle &) {}
Solution 2:
One step Solution.
Alternately, You can overload <<
for vector type itself:
ostream& operator<<( ostream& os, const vector<triangle>&)
{
}
I would personally prefer the Solution 1. It is more readable & Usually more often than not one would already have an overloaded <<
for the vector type being used and it could be leveraged.
error: no match for ‘operator ’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream char ’} and ‘std::_List_iterator int ’)
You've already gotten answers telling you to dereference the iterator:
for(auto currentVertices = currentFace->setofVertices.begin();
currentVertices != currentFace->setofVertices.end();
currentVertices++)
{
cout << *currentVertices; // dereference
}
You could however do that automatically by using a range-based for loop:
for(auto& currentVertices : currentFace->setofVertices) {
cout << currentVertices; // already dereferenced
}
error: no match for 'operator ' (operand types are 'std::istream' {aka 'std::basic_istream char '} and 'Oper')
The problem is that at the point when you wrote:
strm >> wz.lz1 >> wz.op >> wz.lz2;
compiler do not have the definition of the overloaded std::istream& operator >> (std::istream& strm, Oper& t_op)
since it is defined afterwards.
So to solve this just move the definition of std::istream& operator >> (std::istream& strm, Oper& t_op)
before the definition of std::istream& operator >> (std::istream& strm, WyrazenieZespolone& wz)
as shown below. That is for strm >>wz.op
to work, define the corresponding overloaded operator>>
before its use.
Similarly, for strm >> wz.lz1
to work, define the corresponding overloaded operator>>
before its use. This is shown in the below snippet Working demo.
//this comes first so that it can be used in strm >>wz.op
std::istream& operator >> (std::istream& strm, Oper& t_op){
char znak;
strm >> znak;
switch(znak){
case '+': {t_op = op_plus; break;}
case '-': {t_op = op_minus; break;}
case '*': {t_op = op_razy; break;}
case '/': {t_op = op_dziel; break;}
default : {strm.setstate(std::ios::failbit);}
}
return strm;
}
//similarly this comes before so that it can be used in strm >> wz.lz1
std::istream& operator>>(std::istream& strm, LiczbaZespolona&)
{
//do something here
return strm;
}
std::istream& operator >> (std::istream& strm, WyrazenieZespolone& wz){
strm >> wz.lz1 >> wz.op >> wz.lz2;
return strm;
}
Demo
no match for ‘operator=’
If I understand correctly, you're writing something similar to std::unique_ptr
.
So, as for std::unique_ptr
, the operator=()
should works with move semantics, so receiving a r-vale reference, Auto_ptr &&
, not an l-value reference
Auto_ptr2& operator=(Auto_ptr2 && a)
{
std::cout<<"\nAuto_ptr2 operator = called";
if (&a == this)
return *this;
delete m_ptr;
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
return *this;
}
And the use should pass through std::move()
res2=fun(std::move(res2));
Same problem with constructors: avoid copy constructor (maybe delete it) and write a move constructor.
Auto_ptr2 (Auto_ptr2 const &) = delete;
Auto_ptr2 (Auto_ptr2 && a)
{
std::cout<<"\nAuto_PTR2 move constructor called";
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
}
C++ error: no match for 'operator[]' (operand types are 'const my_array_over' and 'size_t' {aka 'long unsigned int'})
In this statement
std::cout << a2[i] << std::endl;
there is used the subscript operator for an object of the type my_array_over
that (the subscript operator) is not defined within the class. It seems you mean
std::cout << a2.get( i ) << std::endl;
Otherwise you need to define the subscript operator within the class definition. For example
const int & operator []( size_t n ) const
{
return a[n];
}
int & operator []( size_t n )
{
return a[n];
}
Why the error no match for 'operator==', when using `std::find`?
The reason for the error message has been well explained in the other answer. I would like to provide a solution to the problem.
As you are trying to find, if any of the std::string
element in the vector of vector matches to "START"
, you could use standard algorithm std::any_of in combination with a unary predicate which returns std::find(vec.cbegin(), vec.cend(), str) != vec.cend()
; where vec
is the each rows of the vector of vectors. See a demo here
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
bool isFound(const std::vector<std::vector<std::string>>& data, const std::string &str)
{
const auto found_in_vector = [&str](const std::vector<std::string> & vec)-> bool {
return std::find(vec.cbegin(), vec.cend(), str) != vec.cend(); // if found
};
if (std::any_of(data.cbegin(), data.cend(), found_in_vector))
{
std::cout << "Found\n";
return true;
}
std::cout << "Missing \""<< str << " \"\n";
return false;
}
int main()
{
std::vector<std::vector<std::string>> data;
std::vector<std::string> test{ "START","test" };
data.emplace_back(test);
std::cout << std::boolalpha << isFound(data, std::string{ "START" } );
}
C++ Error project.cpp:11:20: error: no match for 'operator[]' (operand types are 'std::__cxx11::list int ' and 'int')
In c++, the STL list
doesn't support operator[]
. The reason for this is that it is implemented as a liked list. This means that each item only knows where the elements before and after it are. The list
would look something like this.
Instead of accessing a given part of the list through the brace notation, you want to use a loop to iterate through the list until you reach where you want.
int count = 0;
for(auto iterator = list.begin(); iterator != list.end(); iterator++)
{
if(count == inputNum)
{
// Do whatever you need, or save the iterator to perform operations on
break;
}
}
You can also use the STL
method advance
. It takes in an iterator and the distance you want to move it:
std::list<int>::iterator iterator = list.begin();
std::advance(iterator, numPositions);
C++ no match for operator when printing a std::vector std::pair
You are trying to print out a std::pair
, but there is no standard operator<<
implemented for std::pair
, so you will have to write you own.
Even if you fix that, your code will still fail, because dicVec
is empty when you call std::cout << dicVec[i];
You are not adding dicPair
to dicVec
before printing the contents of dicVec
. In fact, dicVec
should be declared above the loop, not inside of it.
Try something more like this:
typedef std::pair<std::string, std::string> stringPair;
typedef std::vector<stringPair> stringPairVec;
std::ostream& operator<<(std::ostream &out, const stringPair &p) {
out << p.first << " : " << p.second << "\n";
return out;
}
std:ostream& operator<<(std::ostream &out, const stringPairVec &vec) {
for (size_t i = 0; i < vec.size(); ++i) {
out << vec[i];
}
return out;
}
void Translate::read(){
std::ifstream dic("ende.dic");
if (!dic.is_open()) {
throw std::runtime_error("File could not be opened");
}
stringPairVec dicVec;
std::string dicLine;
while (std::getline(dic, dicLine)) {
std::size_t pos = dicLine.find(";");
stringPair dicPair = std::make_pair(
dicLine.substr(0, pos),
dicLine.substr(pos+1)
);
dicVec.push_back(dicPair);
}
std::cout << dicVec;
}
Or, in C++11 and later, you can do this instead:
using stringPair = std::pair<std::string, std::string>;
using stringPairVec = std::vector<stringPair>;
std::ostream& operator<<(std::ostream &out, const stringPair &p) {
out << p.first << " : " << p.second << "\n";
return out;
}
std:ostream& operator<<(std::ostream &out, const stringPairVec &vec) {
for (const auto &p : vec) {
out << p;
}
return out;
}
void Translate::read(){
std::ifstream dic("ende.dic");
if (!dic.is_open()) {
throw std::runtime_error("File could not be opened");
}
stringPairVec dicVec;
std::string dicLine;
while (std::getline(dic, dicLine)) {
std::size_t pos = dicLine.find(";");
dicVec.emplace_back(
dicLine.substr(0, pos),
dicLine.substr(pos+1)
);
}
std::cout << dicVec;
}
Related Topics
How to Use Profile Guided Optimizations in G++
Should I Include <Xxxx.H> or <Cxxxx> in C++ Programs
Relative Performance of Std::Vector VS. Std::List VS. Std::Slist
Iterating C++ Vector from the End to the Beginning
Performance of Qsort VS Std::Sort
Sharing a Global/Static Variable Between a Process and Dll
How to Convert a Reverse Iterator to a Forward Iterator
C++ Sort Keeping Track of Indices
😃 (And Other Unicode Characters) in Identifiers Not Allowed by G++
Cancelling a Thread Using Pthread_Cancel:Good Practice or Bad
How to Call C++ Functions from Within Ruby
Deleted Default Constructor. Objects Can Still Be Created... Sometimes
Does Std::List::Remove Method Call Destructor of Each Removed Element
Error with Multiple Definitions of Function
Move-Only Version of Std::Function
How to Get List of Files with a Specific Extension in a Given Folder