Program nie wypisuje string'a
Ostatnio zmodyfikowano 2018-03-23 16:50
Dawid90 Temat założony przez niniejszego użytkownika |
Program nie wypisuje string'a » 2018-03-23 02:47:39 Witam! program wysypuje się przy wypisaniu domyślnych nazw w funkcjii sedzia(), kompletnie już nie mam na to pomysłu Błąd C2679 dwuargumentowy "<<": nie znaleziono żadnego operatora, który przyjmuje prawostronny operand typu "std::string" (lub nie istnieje akceptowalna konwersja proszę o pomoc oto kod: #include <iostream> #include <string> #include "stdafx.h" #include "Punkt.h" using namespace std;
void sedzia( Punkt pk, Prostokat p ) { if(( pk.x >= p.x ) &&( pk.x <= p.x + p.szerokosc ) &&( pk.y >= p.y ) &&( pk.y <= p.y + p.wysokosc ) ) cout << "punkt " << pk.nazwa << " znajduje sie w prostokacie o nazwie " << p.nazwa << endl; else cout << "punkt " << pk.nazwa << " znajduje sie POZA prostokatem o nazwie " << p.nazwa << endl; }
int main() { Punkt pkt1( "A", 2, 40 ); Prostokat p1( "brak", 1, 1, 3, 6 ); sedzia( pkt1, p1 ); system( "pause" ); return 0; }
#include "stdafx.h" #include "Punkt.h" #include <iostream> #include <string>
using namespace std;
Punkt::Punkt( string n, float xx, float yy ) { nazwa = n; x = xx; y = yy; }
Prostokat::Prostokat( string n, float xx, float yy, float wys, float szer ) { nazwa = n; x = xx; y = yy; wysokosc = wys; szerokosc = szer; }
#include <iostream>
using namespace std;
class Prostokat;
class Punkt { string nazwa; float x, y; public: Punkt( string = "A", float = 0, float = 0 ); void wczytaj(); friend void sedzia( Punkt pk, Prostokat p ); };
class Prostokat { string nazwa; float x, y, wysokosc, szerokosc; public: Prostokat( string = "nowy", float = 0, float = 0, float = 1, float = 1 ); void wczytaj(); friend void sedzia( Punkt pk, Prostokat p ); };
|
|
darko202 |
» 2018-03-23 08:20:21 1. przeczytaj https://stackoverflow.com/questions/22724064/error-c2679-binary-no-operator-found-which-takes-a-right-hand-operand-ofw tym using std::string without including it's header works on some compilers that indirectly import parts of <string> into their <iostream> or other headers but that's not standard and shouldn't be relied upon. Also they often break when you try to output a string since they only included a part of the implementation and are missing the part that implements the operator<<. w niektórych kompilatorach jest opisywany problem -> trzeba go obejść implementując operator << http://www.cplusplus.com/forum/beginner/135127/implementacja operator<< #include <iostream> #include <string> #include <ostream> #include <istream>
struct Student { std::string name; int ID = 0; friend std::istream & operator >>( std::istream & in, Student & cStudent ); friend std::ostream & operator <<( std::ostream & out, Student & cStudent ); };
std::istream & operator >>( std::istream & in, Student & cStudent ) { in >> cStudent.name; in >> cStudent.ID; return in; }
std::ostream & operator <<( std::ostream & out, Student & cStudent ) { out << cStudent.name << "\n\t" << cStudent.ID << "\n"; return out; }
int main() { Student data; std::cin >> data; std::cout << "You entered:\n"; std::cout << data; return 0; }
2. sprawdź cout << p.name.c_str() << "endl;
|
|
Dawid90 Temat założony przez niniejszego użytkownika |
» 2018-03-23 16:50:18 darko202 dziękuje za naprowadzenie i poświęcony czas. o ile dobrze zrozumiałem funkcja iostream nie zna stringa dlatego trzeba przerobić ją na c_str. poprawione: void sedzia( Punkt pk, Prostokat p ) { if(( pk.x >= p.x ) &&( pk.x <= p.x + p.szerokosc ) &&( pk.y >= p.y ) &&( pk.y <= p.y + p.wysokosc ) ) cout << "punkt " << pk.nazwa.c_str() << " znajduje sie w prostokacie o nazwie " << p.nazwa.c_str() << endl; else cout << "punkt " << pk.nazwa.c_str() << " znajduje sie POZA prostokatem o nazwie " << p.nazwa.c_str() << endl; }
|
|
« 1 » |