Class 내에서 Class 정적 객체 생성시 인자를 갖는 생성자 사용시 에러?

hongyver의 이미지

class aa 의 생성자는 기본생성자와 char* a 를 갖는 2가지 생성자가 있습니다.
class bb 에서 정적으로 aa a; 와 같이 선언하여 사용합니다.
때에 따라 기본생성자 말고 char* a를 갖는 생성자를 쓰려고 aa a("test"); 와 같은 사용하면 c2059 구문 오류 라는 에러가 발생합니다.

class *a = new aa("test"); 와 같이 동적 할당을 하면 문제는 없습니다.

이론적으로 이 사항을 어떻게 설명해야 하나요?

구글링해보니
bb(void) : a("test") { printf("bb 생성자\n"); };
와 같이 사용하라고 하는데

이해가 될듯하면서도 안되네요.

aa.h

class aa
{
public:
   aa(void) { printf("aa 생성자1\n"); };
   aa(char* a) { printf("aa 생성자2 : %s\n",a); };
   ~aa(void) { printf("aa 소멸자\n"); };
};

main.cpp

#include "aa.h"
 
class bb
{
public:
   //aa a("test");  // c2059 구문오류 error!!!!
   aa a;
 
   bb(void) { printf("bb 생성자\n"); };
   ~bb(void) { printf("bb 소멸자\n"); };
};
 
int main(int argc, _TCHAR* argv[])
{
   bb b;
}
 의 이미지

Quote:
class bb 에서 정적으로 aa a; 와 같이 선언하여 사용합니다.

멤버 변수 a가 정적 멤버 변수라는 의미인가요?
다시 말해서, 모든 class bb의 인스턴스가 하나의 a를 공유하나요, 아니면 각자 하나씩 가지게 되나요.

후자의 경우라면, 구글링해서 알아내신 바와 같이 class bb의 생성자에서 멤버 변수 a를 초기화해주어야 합니다.
전자의 경우라면, 애초에 지금 멤버 변수 a의 선언 자체가 잘못된 것입니다.
이에 대한 부연설명은 질문자님의 의도를 듣고 나서 드리도록 하지요.

hongyver의 이미지

이게 왜 안되냐고 물어보길래 답변하려고 보니...
갑자기 말문이 막혀 당황스럽더니...
이제사 이게 보이다니 이러고도 내가 개발자라니 ㅜㅜ

변수에 대한 타입을 선언을 해야지. 변수에 대한 정의를 하니 에러가 났던거군요.

헤더에 int i; 를 해야지 int i = 0; 을 했으니...

아무튼 답변 감사했습니다.

--------------------------------------------
오토바이 타는 개발자
홍가일보 편집장 홍가이버

댓글 달기

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