Dracep
Dracep

Reputation: 378

Invalid operands to binary expression after using vector function C++

There seems to be an issue trying to create a string vector function linking with the header file and main function. I successfully ran another string function with the same main, header, and function file.

Please keep in mind that I am a C++ beginner and I have been looking for a solution on other posts for an hour. Any suggestions on my code are appreciated as well.

Header:

#ifndef splitter
#define splitter

#include <iostream>
#include<string>
#include<vector>
#include<cctype>
using std::cout;using std::endl;using std::cin;
using std::vector;
using std::string;

vector<string> split (const string &s, char separator=' ');

#endif

Main:

#include"functions.h"

#include <iostream>
using std::cout;using std::endl;using std::cin;
#include<string>
using std::string;
#include<vector>
using std::vector;

int main() {

cout << split("test", 'a') << endl;

return 0;
}

Functions:

#include "functions.h"

#include <iostream>
#include<string>
#include<vector>
using std::cout;using std::endl;using std::cin;
using std::vector;
using std::string;

vector<string> split(const string &s, char separator) {
    vector<string> thing;
    thing.push_back(s);
    return thing;
}

My error message:

Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream') and 'vector' (aka 'vector, allocator > >'))

Upvotes: 1

Views: 1389

Answers (1)

R Sahu
R Sahu

Reputation: 206627

There is no function to insert a std::vector<std::string> to cout. Hence the line

cout << split("test", 'a') << endl;

is a problem. Change it to:

auto res = split("test", 'a');
for ( auto const& item : res )
{
   std::cout << item << " ";
}
std::cout << std::endl;

Upvotes: 2

Related Questions