구조체를 전역변수로 사용하기 위해서는..

facered79의 이미지

file1.cpp 에 구조체를 전역으로 선언하고.

file2.cpp 에서 위 구조체에 접근 하기 위해서는

extern 을 사용해야 하는것인가요..

flie2.cpp 에

extern struct Type initType;

이라고 사용하면

`initType' has incomplete type

라는 에러가 발생합니다..

이문제의 해결 방법에 대한 조언 부탁 드리겠습니다..

Fe.head의 이미지

file.h 파일을 만드신후에

file.h에 struct를 선언후에

file1.cpp 에서 전역으로 선언
file2.cpp 에서 extern으로 선언해 보세요.

그런데 되도록이면 extern쓰지마시고

전역 변수를 건드리는 함수를 만들어 쓰는것을 추천합니다.

그러면 굳이 extern 쓸 필요 없죠.

고작 블로킹 하나, 고작 25점 중에 1점, 고작 부활동
"만약 그 순간이 온다면 그때가 네가 배구에 빠지는 순간이야"

facered79의 이미지

답변 해주신 내용을 보면..

지금 제가 한 방법과 차이가 없어보여서요..

file.h 를 만든후에 struct 구조체를 정의한후에

file1.cpp 에 전역으로 선언..

그후 file2.cpp 에서 extern 을 사용하는것과..

file1.cpp 에 struct 를 정의하고 그 아래에 전역으로 선언한후

file2.cpp 에서 extern 을 사용하는것과는 차이가 없어 보입니다..

쉽게 예를 들어

file1 에 int i; 라는 전역을 선언하고

file2 에 extern int i;

를 사용하면 i 의 값을 사용할 수 있듯 구조체를 사용할려고 합니다..

어찌해야 할까요..

facered79의 이미지

 //file1.cpp

#include <iostream>
#include "struct2.hh"

using namespace std;

struct RfmbRdabSlotType {
        long slotNum;
        long eqStatus;
        long blkStatus;
        long ipmbAddress;
        long slotType;
        long alarmType;
        long alarmGrade;
        string alarmCause;
        string date;
};


struct RfmbRdabInitDbType{
    struct RfmbRdabSlotType slotType1[];
}initDb;


int main()
{
    initDb.slotType1[2].slotNum=2;
    cout<<initDb.slotType1[2].slotNum<<endl;
    print();
    return 0;
}

~
~

 //file2.cpp

#include <iostream>
#include "struct2.hh"
using namespace std;

extern struct RfmbRdabInitDbType initDb;

void print()
{
    cout<<"다른 소스"<<initDb.slotType1[2].slotNum<<endl;
}

소스 내용은 위와 같습니다..


pynoos의 이미지

각각을 컴파일 할 때 -save-temps 옵션을 줘서 생기는
file2.ii 파일을 열어서 비교해 보세요.

facered79의 이미지

*.ii 란 파일은 얻었는데..

파일 내용을 비교 하는것이 무슨 뜻인지 모르겠네요..

제가 올린 소스 대로라면

file1.cpp 에서 선언하고 일부 정의 한 전역 변수(구조체)를

file2에서 전역변수 의 값을 수정하고 갱신할수 있는 것인지요...

pynoos의 이미지

제가 쉬운말을 너무 어렵게 했나 봅니다.

Quote:
`initType' has incomplete type

struct 선언이 반드시 되어야 사용할 수 있기 때문에 .h 파일에 두고 extern 전에 그 .h를 include 해야하는 것을 보여주고자 함이었습니다.

.ii 는 c++ 용 전처리 결과가 저장되는 temp 파일인데, .h를 두어 하는 것과 하지 않는 것의 .ii 파일을 살펴보면 선언부가 extern 보다 앞에 있고/없고를 확인할 수 있을 것입니다.

jeweljar의 이미지

쉽게 생각하세요.

file1.cpp 에 있는

struct RfmbRdabSlotType { 
        long slotNum; 
        long eqStatus; 
        long blkStatus; 
        long ipmbAddress; 
        long slotType; 
        long alarmType; 
        long alarmGrade; 
        string alarmCause; 
        string date; 
};

구조체 정의가
file2.cpp 를 컴파일할때는 보이지 않으니 문제가 발생하겠지요.

위의 구조체 정의를 struct2.hh 에 넣으시면
file1.cpp, file2.cpp 모두 struct RfmbRdabSlotType 를 이해할 수 있게됩니다.

댓글 달기

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