string을 리턴하는 함수를 헤더파일로 나누었을 때 오류

sangjin byun@Google의 이미지

main.cpp

#include <iostream>
#include <string>
 
using namespace std;
 
string a() {
	string test = "test";
 
	return test.substr();
};
 
int main() {
	cout << a() << endl;
	return 0;
}

위의 코드같은 경우에는 잘 작동 됩니다.

근데 string a()를 헤더파일을 만들어서
main.cpp

#include <iostream>
#include <string>
#include <item.h>
 
using namespace std;
 
int main() {
	cout << a() << endl;
	return 0;
}

item.h

#pragma once
 
string a();

item.cpp
#include "item.h"
#include <string>
 
using namespace std;
 
string a() {
	string test = "test";
 
	return test.substr();
};

같이 만들경우 컴파일러에서
"반환 형식으로만 구분되는 함수를 오버로드할 수 없습니다."라고 하는데

main에서 사용하던걸 헤더파일로 불러왔을 뿐인데 왜이러는지 이해를 할 수 없습니다...

Stephen Kyoungwon Kim@Google의 이미지

STL의 string은 std라는 namespace 내에 있습니다. 그냥 string이라고 하면 STL의 string이 아닙니다. using namespace std; 라는 라인은 이 라인 이후의 심볼이 뭔지 모르겠거든 거기다 std:: 를 앞에 붙여 보라는 의미입니다 (대충 말해서). 그 라인 전의 모든 심볼에겐 적용이 되지 않죠.

include "item.h"가 그 라인 앞에 있기 때문에 헤더에서 온 declaration은 지금 전부 그냥 string인데, definition 부분은 std::string입니다.

item.cpp의 경우 definition과 declaration의 다른 모든 게 같은데 리턴 타입만 다른 셈이 되죠. 그래서 리턴 타입만 같은 오버로딩은 허용되지 않는다고 하는 것 같습니다. 오버로딩은 패러미터 타입이 다르고 이름은 같아야 합니다.

헤더에 string이 사실 std::string이기 때문에 여기에 다음이 먼저 들어가야 합니다.

#pragma once 
 
#include <string>
using std::string; // from now on, std::string is the same as string
string a();

sangjin byun@Google의 이미지

어우 명확한 답변 감사합니다 ^^.

익명 사용자의 이미지

대체로 동의드립니다만, 저라면 item.h를 아래와 같이 작성할 것입니다.

#pragma once
 
#include <string>
 
std::string a();
Stephen Kyoungwon Kim@Google의 이미지

그리고 모든 .cc 나 .cpp 파일에서 std::string이라고 쓰고요. 저도 그렇게 합니다.
그런데 그 정도는 대단한 것도 아니고 이러나 저러나 상관없다고 생각합니다. 코드를 읽고 쓰는 사람들이 알아서 할 일이고요.

댓글 달기

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