숙제가.ㅠㅠ 클래스배열 질문합니다!!

shy821의 이미지

class data{
public:
int *name;
int score;
};

int main(){
data **db = new data*[10];
for(int i=0;i<10;i++)
db[i] = new data[10];

for(int i=0;i<3;i++)
for(int j=0;j<10;j++)
db[i][j].score=0;

for (int i=3;i<5;i++)
for(int j=0;j<2*10;j++){
db[i][j].score = 1;
j++;
if(j==10){
data *temp = new data[2*10];
copy(&(db[i][0]), &(db[i][j-1]), temp);
db[i]=temp;
}
}

for( int i=5;i<10;i++)
for(int j=0;j<10;j++)
db[i][j].score=i;
return 0;
}

구현하려는 부분과 대충 비슷한 예제를 짜봤습니다. 자잘한 문법미스 같은거는 패스 ㅋ

그러니까 동적2차원클래스 배열에서요.. 특정 행의 열만을 두배로 늘리려고 하는데요 (위에서 copy 부분)

저런식으로 쓰면 에러나는군요(문법오류 말고요)... 어떻게 해야 하나요??

도와주세요ㅠㅠ

-- 죄송합니다.. 자꾸 글이 이상하게 떠서 조금 수정했습니다

NK의 이미지

일단 다른 부분은 잘라내고 필요한 부분만 정리해 봤습니다.
보기가 조금 힘들어서 들여쓰기도 넣었습니다.

for(int i=3;i<5;i++)
 for(int j=0;j<2*10;j++){
  db[i][j].score = 1;
  j++;
  if(j==10){
   data *temp = new data[2*10];
   copy(&(db[i][0]), &(db[i][j]), temp);
   db[i]=temp;
  }
 }

temp를 집어넣기 전에 db[i]에 있는걸 delete하지 않았다는 점은 둘째치고,
if(j==10)은 절대로 true가 될 수 없는 조건식입니다.
최초로 j=0 으로 시작하지만, if에 비교될 때에는 j=1이지요.
그 다음 for문을 다시 시작하면서 j=2에 기록되고, 비교는 j=3 입니다.
즉, if문에서 비교되는 숫자는 전부 '홀수' 입니다. 10은 나올수가 없지요.
if의 조건문을 제대로 쓰려면 (j>=10) 정도가 되겠지요.

그리고 값을 대입하는 부분은 가급적이면 배열을 확장하는 부분보다 나중에 넣는걸 권합니다.
혹시라도 조건이 바뀌어서 j가 기본 capacity보다 크게 나와버린다면 현재 상태로는
확장되기 전에 기록을 시도하기에 문제가 생기게 됩니다.

from nightknight

댓글 달기

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