구조체 관련 문의드립니다.

zkapdf_1004의 이미지

안녕하세요
구조체 선언 후 그 값을 리턴해주려고하는데 에러가 나네요
왜 구조체를 인식못하는것인지 조언부탁드립니다

header file

class Main{
public:
   Main()
   ~Main();
 
public:
   struct Test_ST {
      int count;
      int index; 
   };
 
  Test_ST  GetInfo(); ==> 에러 없음
}
 
cpp file
Test_ST*  Main::GetInfo()  ==> Test_ST가 선언되지않았다고 에러가 발생합니다.
{
    Test_ST  *pTest = new Test_ST();
    pTest->count = 1;
    pTest->index = 1;
 
    return pTest;
}

header file에 구조체를 선언하고 그 구조체를 return 해주려고하는데요...
왜 인식을 못하는건가요?
라스코니의 이미지

혹시 Main::Test_ST *Main::GetInfo()

이거일까요? 그리고 *를 추가하셔야죠

zkapdf_1004의 이미지

*표시 못한건 여기 글쓰면서 실수로 빠트렸네요... Main::Test_ST*로 하면 정상동작합니다...
왜 Main::Test_ST이렇게 선언을 해야만 가능한건지 궁금해서 글 올린거였어요....

라스코니의 이미지

단순하게 생각하시면 되요. 컴파일러가 보는 scope 안에 Test_ST가 들어오는지..
전역에 선언하면 당연히 아무런 다른 조치를 안해도 컴파일러가 보게 되고요.
지금처럼 클래스 안에 선언하게 되면 컴파일러가 모든 클래스, 다른 헤더를 다 뒤져서 찾을 수도 없죠.

using namespace std; 를 쓰는 것과 마찬가지 입니다.

한번 using namespace Main; 한후 원래대로 해보세요.
별로 권장하지 않는 방법입니다. 굳이 전역적으로 공개하고 싶었으면 전역적으로 선언했으면 되는 거구요. 클래스의 public 내에 둔건 반드시 명시적으로 Main 클래스의 scope에서만 구조체를 사용하기 싶다는 의지를 반영하는 것이라고 봅니다.

익명 사용자의 이미지

using namespace Main; 해 봤나요? 그게 되던가요...

종종 틀린 소리를 너무 당당하게 해서 되려 헷갈리게 만드는 답변자가 있네요.

익명 사용자의 이미지

저는 이 문제가 단순하다고 생각하지 않습니다.

Test_ST는 명백히 Main의 class scope 안에서 선언되었습니다.
따라서 해당 scope 안에서만 unqualified name, 즉 Test_ST으로 부를 수 있습니다.

그런데 멤버 함수 Main::GetInfo의 정의는 class scope 밖에 있는 것으로 보입니다.
이 경우, class scope는 멤버 함수의 정의를 포함하도록 확장됩니다.
그래서 멤버 함수 Main::GetInfo안에서 지역 변수를 Test_ST *pTest와 같이 선언할 수 있었던 겁니다.

그런데 여기에 단서가 하나 더 붙습니다.
class scope에 포함되는 건, 정확히는 멤버 함수의 declarator-id부터입니다

반환값 타입은 declarator-idMain::GetInfo에 있기 때문에 해당 사항이 없죠.
따라서 Main::Test_ST라고 qualified name을 써 주어야 합니다.

======

C++11에서 추가된 trailing return type을 쓰면 위와 같은 상황에서 반환값도 unqualified name으로 지정할 수 있게 됩니다.

class Main{
public:
	struct Test_ST {
		int count;
		int index; 
	};
 
  	Test_ST *GetInfo();
};
 
auto Main::GetInfo() -> Test_ST *{
	Test_ST *pTest = new Test_ST();
	pTest->count = 1;
	pTest->index = 1;
 
	return pTest;
}

======

옆에 C++20 표준 문서를 띄워 놓고 보면서 쓰고 있는데도 어렵네요.
제대로 설명하려면 declarative region이나 potential scope 같은 거 다 짚고 넘어가야 하는데 너무 복잡해서 대충 얼버무려서 요약했습니다.

정확하게 알고 싶으면 표준문서 6.4 Scope (basic.scope), 6.5.2 Unqualified name lookup (basic.lookup.unqual) 읽으세요.

Anti-Lock의 이미지

감사합니다.
덕분에 하나 알아갑니다.
'trailing return type'

댓글 달기

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