• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

C2676 에러

19.10.20 23:13 작성 조회수 582

2

C2679 binary '<<': no operator found which takes a right-hand operand of type 'const Position2D'

위의 에러가 떠서 해결이 되지 않습니다.

원인이 뭘까요?

컴파일러 버전 탓인지.. VS2015 쓰고있습니다. 

// Monster.h

#pragma once
#include "Position2D.h"


class Monster
{

private:
	std::string m_name; 
	Position2D m_location; 
public:
	Monster(const std::string name_in, const Position2D & pos_in)
		: m_name(name_in), m_location(pos_in)
	{}

	void moveTo(const Position2D & pos_target)
	{
		m_location.set(pos_target);
	}

	friend std::ostream & operator  << (std::ostream & out, const Monster & monster)
	{
		out << monster.m_name << "  " << monster.m_location; // m_,location으로 변경 깔끔해짐
		return out;
	}

};


//Position2D.h
#pragma once
#include <iostream>
#include <string>

class Position2D
{
private:
	int m_x;
	int m_y;

public:
	Position2D(const int & x_in, const int & y_in)
		:m_x(x_in), m_y(y_in)
	{}

	void set(const Position2D & pos_target)
	{
		set(pos_target.m_x, pos_target.m_y); 
	}
	void set(const int & x_target, const int & y_target)
	{
		m_x = x_target;
		m_y = y_target;
	}

	friend std::ostream & operator << (std::ostream & out, Position2D & pos2d)
	{
		out << pos2d.m_x << "  " << pos2d.m_y << std::endl;
		return out;
	}

};


// Monster.cpp


#include "Monster.h"

using namespace std;

int main()
{

	Monster mon1("jack", Position2D(0, 0));

	{
		mon1.moveTo(Position2D(1, 4));
		cout << mon1 << endl;
	}

	return 0;

}

답변 3

·

답변을 작성해보세요.

1

제가 컴파일 해봤더니 아래와 같은 에러 메시지를 찾을 수 있었습니다. 초보들은 에러 메시지가 잔뜩 나오면 살펴보지 않는 경향이 있는데 에러 메시지를 잘 보시면 대부분 해결하실 수 있습니다.

main.cpp:55:44: error: binding ‘const Position2D’ to reference of type ‘Position2D&’ discards qualifiers

참고로 테스트는 온라인 컴파일러에서 gcc로 진행했기 때문에 visual studio에서는 에러 메시지가 조금 다르게 나올 수 있습니다.

0

홍길동님의 프로필

홍길동

2021.01.01

덕분에 저도 다시 배우고 갑니다

0

tndyd8282님의 프로필

tndyd8282

질문자

2019.10.21

감사합니다. 교수님 

바쁘실텐데 컴파일까지 해서 검토해주셔서 감사합니다. 완벽하게 이해하였습니다.

에러를 잘 읽어보니 문제는 const 로 선언된 Monster 의 매개변수를 << 연산자 오버로딩해서 Position2D를 출력하기 위해서는 position2D의 연산자 오버로딩 함수의 파라미터를 const Position2D &로 선언을 했었어야 했네요.

컴파일러는 컴파일 시 문제가 된 부분을 잡아내는 것이지 디버깅을 위해 수정을 해야하는 부분을 잡아내는 것이 아니라는 것을 확실히 이해하게 되었네요. 

감사합니다.