c++ 파생클래스 질문입니다.

jbssy의 이미지

안녕하세요 c++ 기초 플러스 4판을 보며 C++을 공부하고 있습니다.

동적 메모리 할당을 하는 기초클래스 baseDMA 가 있고, 역시 동적 메모리할당을 하는 파생 클래스 hasDMA가 있다고 했을 때 각각의 대입연산자 정의를 아래와 같이 해주고 있습니다.

baseDMA & baseDMA::operator=(const baseDMA & rs)
{
	if (this == &rs)
		return *this;
	delete [] label;
	label = new char[strlen(rs.label) + 1];
	strcpy(label, rs.label);
	rating = rs.rating;
	return *this;
}
.....
 
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
	if (this == & hs)
		return *this;
	delete [] style;
	<span>baseDMA::operator=(hs);</span> //기초 클래스 부분을 복사한다. 
	style = new char[strlen(hs.style)+1];
	strcpy(style, hs.style);
	return *this;
}

볼드체로 되어있는 부분을 주석처리하면 대입연산자가 실행될 때, baseDMA 부분은 디폴트 생성자로 생성이 잘 됩니다. 그런데 문제는 저 기초 클래스 부분을 복사하는 baseDMA::operator=(hs); 입니다. 책 예제에는 이렇게 나와 있는데, 똑같이 작성하고 g++ 로 컴파일 하고 실행시켰을 때, 메모리에 관련된 에러를 생성하더군요.

그 사이에 표준이 바뀐건가요? 아니면 제가 뭘 잘 못한 걸까요. -_-;

전체 소스파일하고 로그파일도 같이 첨부 시킵니다. 답변 기둘리고 있겠습니다.

File attachments: 
첨부파일 크기
Binary Data dma.tar.gz1.78 KB
fracktal의 이미지

오타 입니다..

label = new char[strlen(rs.label + 1)];

를 고치세요..

----
'아.... 하루종일 놀고 싶다...'


'아.... 하루종일 놀고 싶다...'

jbssy의 이미지

근데.. 저건 이 글 쓰다 난 오타구요... ^^
첨부파일엔 제대로 되어 있습니다. g++로 컴파일도 제대로 되는데 실행이 안됩니다. -_-

참고로 C++ 기초 플러스 알파 4판. 13장. 클래스의 상속 listing: 13.13 ~ 13.15 예제에 그대로 있는 내용입니다.

LINUH DESKTOP - Never be alone again

kaeri17의 이미지

baseDMA & baseDMA::operator=(const baseDMA & rs)
첨부파일에도 이부분에 오타 있는데요. label = new char[strlen(rs.label + 1)];를 label = new char[strlen(rs.label) + 1];로 고치니 에러가 없군요

jbssy의 이미지

제가 style = new... 부분에 오타가 없는 걸 잘못 확인 했군요.

죄송합니다. 상당히 면목없네요.. -_-;

이 밀려오는 쪽팔림이란... -_-;;

LINUH DESKTOP - Never be alone again

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.