Visual studio: E0349 no operator << matches these operands (but no strings in code)

5.7k views Asked by At

Can't compile my code - I get: no operator “<<” matches these operands

I found similar problem no operator "<<" matches these operands, however I have no strings nor missing directives (i think)

Could someone help me please? :)

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip> 
#include <vector>

void convBase(int base, int n);

using std::cout; 
using std::endl; 
using std::setw;
using std::vector;

int main()
{

    int numbs[] = { 61, 0, -9, -200, 9999 };
    int bases[] = { 16, 8, 2, 20, 36 };
    size_t size = sizeof(numbs) / sizeof(*numbs);

    for (size_t i = 0; i < size; ++i) {
        cout << setw(4) << numbs[i] << " = " << setw(5) 
        << convBase(numbs[i], bases[i]) << " in base " 
        << setw(2) << bases[i] << endl;
        }
    return 0;

}

void convBase(int n, int base) {
    char pierwszyZnak = 48;
    char pierwszaLitera = 65;
    vector<char> system;
    vector<char> liczba;

    for (int i = 0; i < base; i++) {
        if (i <= 9) {
            system.push_back(pierwszyZnak);
            pierwszyZnak++;
        }
        else if (i <= 36) {
            system.push_back(pierwszaLitera);
            pierwszaLitera++;
        }
        else {
            cout << "podales za duza liczbe: " << base << ". Musisz podac liczbe mniejsza badz rowna 36" << endl;
            return;
        }
    }

    while (n > 0) {
        int rem = n % base;
        int rem2 = floor(n / base);
        liczba.push_back(system[rem]);
        n = rem2;
    }

    for (unsigned int i = liczba.size(); i-- > 0; )
        std::cout << liczba[i];
}
1

There are 1 answers

0
Jeff Hiner On

convBase returns void, but you're trying to stream its return value to std::cout. Your function should return a string representation, silently. Return a std::string or std::ostream instead of void.

I'd suggest creating a std::stringstream and then streaming your output to that. You can replace the very last std::cout with the name of your stream, and then call its str() method to get the return value. (You should also make sure to verify the function behaves predictably when you give it bad characters.)