C2676 에러
994
작성한 질문수 5
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
감사합니다. 교수님
바쁘실텐데 컴파일까지 해서 검토해주셔서 감사합니다. 완벽하게 이해하였습니다.
에러를 잘 읽어보니 문제는 const 로 선언된 Monster 의 매개변수를 << 연산자 오버로딩해서 Position2D를 출력하기 위해서는 position2D의 연산자 오버로딩 함수의 파라미터를 const Position2D &로 선언을 했었어야 했네요.
컴파일러는 컴파일 시 문제가 된 부분을 잡아내는 것이지 디버깅을 위해 수정을 해야하는 부분을 잡아내는 것이 아니라는 것을 확실히 이해하게 되었네요.
감사합니다.
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
466
1
메모리 주소 10진수로 출력
1
653
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
499
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
534
1
메모리 주소에 관한 질분
0
679
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
549
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
443
1
Digit 뒤에 reference를 사용하는 이유
0
510
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
323
1
dat파일이...
0
539
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
644
1
복사 생성자 관련 질문이 있습니다.
0
454
1
수업 중 궁금한점이 있습니다.
1
390
1
라이브러리자체가 이해가 되지 않습니다.
0
561
1
마지막 예제 질문
0
302
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
375
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
412
1
friend함수 관련 질문이 있습니다.
0
312
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
447
1
3분 17초 질문
0
350
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
448
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
445
2
const Something &st에서 const를 빼면 안되나요?
0
300
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
493
1





