에러를 못잡겠습니다.

nayana의 이미지

#ifndef BASE1_H
#define BASE1_H

class Base1
{
public :
	Base1( int parameterValue )
	{
		value = parameterValue;
	}

	int getData() const {	return value; }

protected :
	int value; // 파생 클래스에 의해 상속
};

#endif

#ifndef BASE2_H
#define BASE2_H

class Base2
{
public :
	Base2( char characterData )	{ letter = characterData; }
	char getData() const	{ return letter; }

protected :
	char letter; // 파생 클래스에 의해 상속
};

#endif

#ifndef DERIVED_H
#define DERIVED_H

#include <iostream>

using std::ostream;

#include "base1.h"
#include "base2.h"

class Derived : public Base1, public Base2
{
public :
	Derived( int, char, double  );
	double getReal() const;

private :
	double real;
};

#endif

#include "derived.h"

Derived::Derived( int integer, char character, double double1 )
	:Base1( integer ), Base2( character ), real( double1 )
{

}

double Derived::getReal() const { return real; }

ostream &operator << ( ostream &output, const Derived &derived )
{
	output << "		Integer: " <<   derived.value
		   << "\n   character: " << derived.letter
		   << "\nReal number: " <<  derived.real;

	return output;
}		

#include <iostream>

using std::cout;
using std::endl;

#include "base1.h"

#include "base2.h"
#include "derived.h"

int main ( void )
{
	Base1 base1( 10 ), *base1Ptr  = 0;
	Base2 base2( 'Z' ), *base2Ptr = 0;

	Derived derived( 7, 'A', 3.5  );

	cout << "object base1 contains integer "
		<< base1.getData()
		<< "\nObject base2 contains character "
		<< "\nObject derived contains: \n" << derived << "\n\n";

	cout << "Data members of Derived can be"
		<< "accessed individually: "
		<< "\n		Integer: " << derived.Base1::getData()
		<< "\n	Character  : " << derived.Base2::getData()
		<< "\nReal number  : " << derived.getReal() << "\n\n";

	cout << "Derived can be breated as an "
		<< "object of either base class: \n";

	base1Ptr = &derived;
	cout << "base1Ptr->getData() yields "
		<< base1Ptr->getData() << '\n';

	base2Ptr = &derived;
	cout << "base2Ptr->getData() yields "
		<< base2Ptr->getData() << endl;

	return 0;
}

에러무작위로 쏟아 집니다.

fig.cpp: In function `int main()':
fig.cpp:21: no match for `std::basic_ostream<char, std::char_traits<char> >& <<
   Derived&' operator
/usr/include/c++/3.2.2/bits/ostream.tcc:55: candidates are:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(std::basic_ostream<_CharT,
   _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
   _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:77:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(std::basic_ios<_CharT,
   _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits
   = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:99:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char,
   _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:171:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:208:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:146:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(bool) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:104:                 std::basic_ostream<_CharT,
   _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with
   _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:115:                 std::basic_ostream<_CharT,
   _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned
   int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:119:                 std::basic_ostream<_CharT,
   _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT
   = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:130:                 std::basic_ostream<_CharT,
   _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int)
   [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:234:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long long int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:272:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:298:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(double) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:145:                 std::basic_ostream<_CharT,
   _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with
   _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:323:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long double) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:348:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(const void*) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:120:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT =
   char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:251:                 std::basic_ostream<char,
   _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned
   char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:246:                 std::basic_ostream<char,
   _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed
   char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:622:
   std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
   _Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:572:
   std::basic_ostream<_CharT, _Traits>&
   std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with
   _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:227:                 std::basic_ostream<char,
   _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
   [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:222:                 std::basic_ostream<char,
   _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
   [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:500:
   std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
   _Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:211:                 std::basic_ostream<_CharT,
   _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with
   _CharT = char, _Traits = std::char_traits<char>]
fig.cpp:14: warning: unused variable `Base2 base2'
base1.h: In function `std::ostream& operator<<(std::ostream&, const Derived&)':
base1.h:15: `int Base1::value' is protected
derived.cpp:14: within this context
base2.h:11: `char Base2::letter' is protected
derived.cpp:15: within this context
derived.h:18: `double Derived::real' is private
derived.cpp:15: within this context

지금 책 예제 따라 하면서 공부하고 있는데...무작위로 쏟아 집니다.^^;

doldori의 이미지

표준 라이브러리를 잘못 써서 생기는 에러 메시지는 보통 이 모양입니다. :)
정신 없죠. 그래도 자꾸 에러 메시지를 읽는 연습을 하면 조금씩 익숙해집니다.
힌트는 맨 마지막에 나오네요.

Quote:
base1.h: In function `std::ostream& operator<<(std::ostream&, const Derived&)':
base1.h:15: `int Base1::value' is protected
derived.cpp:14: within this context
base2.h:11: `char Base2::letter' is protected
derived.cpp:15: within this context
derived.h:18: `double Derived::real' is private
derived.cpp:15: within this context

ostream &operator << () 함수에서 클래스의 private/protected 멤버에
접근했기 때문에 나는 에러입니다. 해결책은 두 가지를 생각할 수 있습니다.

1. ostream &operator << ()를 Derived 클래스의 friend로 선언한다.

class Derived : public Base1, public Base2 
{
    friend ostream &operator << ( ostream &output, const Derived &derived );
};

2. 각 클래스에 멤버의 값을 돌려주는 public 멤버함수가 있으므로 이를 이용한다.
그런데 Base1과 Base2에 getData() 함수가 모두 있어 모호함(ambiguity)을
발생하므로 클래스의 이름을 명시한다.

ostream &operator << ( ostream &output, const Derived &derived ) 
{ 
    output << "      Integer: " <<  derived.Base1::getData()
           << "\n   character: " << derived.Base2::getData()
           << "\nReal number: " << derived.getReal();

   return output; 
}  

private/protected 멤버에 대한 접근은 최소한으로 줄이는 것이 좋으므로
2번이 더 좋은 방법이라고 봅니다.

nayana의 이미지

감사합니다. 미쳐 왜 이걸 생각하지 못했는지 부끄럽네요^^
즐거운 하루 되세요!!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.