class 간의 변수 공유에 대해서 질문 입니다...

cdcmp의 이미지

안녕하세요...

다음과 같이 두개의 class를 사용하고 있습니다...

class Thread : QThread
{
    public:

    private:
} 

class Trans : public Dialog
{
    public:

    private:
}

void Thread::run()
{
   cnt++;
}

void Trans::start()
{
   
}

대략 위의 코드와 같이 cnt가 증가 되는 값을 void Trans::start()

함수에서 cnt를 읽어야 하는데요...

그럼 cnt가 전역 변수로 선언이 되어야겠는데요...

class간 변수를 공유하기 위해서 전역변수 선언을 어디서 어떻게 해야 하는지

요??

class간 변수 공유를 해보지를 않아서.....벽에 부딪치고 말았습니다...

답변 부탁드리겠습니다...

익명 사용자의 이미지

멤버 변수를 static 으로 선언하면 됩니다.
static 멤버 변수 관련해서 C++ 문법책을 잘 뒤져보시면 잘 나와있을 겁니다.

cdcmp의 이미지


///thread.h////
class Thread : QThread
{
    public:
      static int cnt;     

    private:
};
int Thread::cnt;

////trans.h////
class Trans : public Dialog
{
    public:

    private:
};


/////thread.cpp/////
void Thread::run()
{
   cnt++;
}


/////trans.cpp/////
void Trans::start()
{
   
} 

위의 코드와 같이 static으로 정의를 했습니다...

그리고 컴파일 하니 다음과 같은 에러가 났습니다...

thread.o(.bss+0x4):/home/eunsu/serial/sensor/threads/thread.cpp:7: multiple definition of `Thread::cnt'
main.o(.bss+0x0):/home/eunsu/serial/sensor/threads/main.cpp:6: first defined here
threadform.o(.bss+0x0):/home/eunsu/serial/sensor/threads/threadform.cpp:9: multiple definition of `Thread::cnt'
main.o(.bss+0x0): first defined here
moc_threadform.o(.bss+0x0):/home/eunsu/serial/sensor/threads/moc_threadform.cpp:20: multiple definition of `Thread::cnt'
main.o(.bss+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [threads] 오류 1

에러가 왜 생기는지..... 다시 한번 답변 부탁 드릴께요...

progcom의 이미지

cdcmp wrote:

///thread.h////
class Thread : QThread
{
    public:
      static int cnt;     // 이 시점에서 Thread::cnt 가 선언되었습니다.

    private:
};
int Thread::cnt; // 여기서 또 선언하니까 중복 선언한다고 화냅니다. 초기화라면 Thread::cnt = 0 같은 식으로 해야겠지요.

(중략)

thread.o(.bss+0x4):/home/eunsu/serial/sensor/threads/thread.cpp:7: multiple definition of `Thread::cnt'
main.o(.bss+0x0):/home/eunsu/serial/sensor/threads/main.cpp:6: first defined here
threadform.o(.bss+0x0):/home/eunsu/serial/sensor/threads/threadform.cpp:9: multiple definition of `Thread::cnt'
main.o(.bss+0x0): first defined here
moc_threadform.o(.bss+0x0):/home/eunsu/serial/sensor/threads/moc_threadform.cpp:20: multiple definition of `Thread::cnt'
main.o(.bss+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [threads] 오류 1

에러가 왜 생기는지..... 다시 한번 답변 부탁 드릴께요...

multiple definition of `Thread::cnt'

중복 선언! 이라고 컴파일러가 친절하게 설명해줍니다. 질문 하기 전에 에러 메세지를 10분만 쳐다보며 생각해보는 것도 좋습니다.[/b]

익명 사용자의 이미지

int Thread::getCount()
{
     return cnt;
}

댓글 달기

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