명품 C++ 프로그래밍 4장 실전문제 9번 질문입니다

minmin999333의 이미지

문제
-------------------------------------------------------------------------------------------------
다음과 같은 Person 클래스가 있다.

Person 클래스와 main() 함수를 작성하여, 3개의 Person 객체를 가지는 배열을 선언하고, 다음과 같이 키보드에서 이름과 전화번호를 입력받아 출력하고 검색하는 프로그램을 완성하라.
-------------------------------------------------------------------------------------------------

class Person{
    string name;
    string tel;
public:
    Person();
    string getName() { return name; }
    string getTel() { return tel; }
    void set(string name, string tel);
};

-------------------------------------------------------------------------------------------------
내가 짠 코드
-Class.h-
#pragma once
class Person {
	string name;
	string tel;
public:
	Person() {};
	string getName() { return name; }
	string getTel() { return tel; }
	void set(string name, string tel);
};

-Class.cpp-
#include "Class.h"
void Person::set(string n, string t) {
	name = n;
	tel = t;
}

-main.cpp-
#include<iostream>
#include<string>
#include"Class.h"
using namespace std;
class Person {
	string name;
	string tel;
public:
	Person() {};
	string getName() { return name; }
	string getTel() { return tel; }
	void set(string name, string tel);
};
void Person::set(string n, string t) {
	name = n;
	tel = t;
}
int main() {
	string name, tel, temp;
	Person* p = 0;
	p=new Person[3];
	cout << "이름과 전화 번호를 입력해 주세요" << endl;
	for (int i = 0; i < 3; i++) {
		cout << "사람 " << i + 1 << ">> ";
		cin >> name >> tel;
		p[i].set(name, tel);
	}
	cout << "모든 사람의 이름은 ";
	for (int i = 0; i < 3; i++)
		cout << p[i].getName() << " ";
	cout << endl;
	cout << "전화번호 검색합니다. 이름을 입력하세요>>";
	cin >> temp;
	cout << "전화 번호는 ";
	for (int i = 0; i < 3; i++) {
		if (temp.compare(p[i].getName()) == 0) {
			cout << p[i].getTel();
		}
	}
	delete[]p;
}

이렇게 짠 후에 빌드를 했는데 오류가
심각도 코드 설명 프로젝트 파일 줄 비표시 오류(Suppression) 상태
오류 C2027 정의되지 않은 형식 'Person'을(를) 사용했습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 37
오류 C3646 'name': 알 수 없는 재정의 지정자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 4
오류 C4430 형식 지정자가 없습니다. int로 가정합니다. 참고: C++에서는 기본 int를 지원하지 않습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 4
오류 C3646 'tel': 알 수 없는 재정의 지정자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 5
오류 C4430 형식 지정자가 없습니다. int로 가정합니다. 참고: C++에서는 기본 int를 지원하지 않습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 5
오류 C3646 'getName': 알 수 없는 재정의 지정자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 8
오류 C2059 구문 오류: '(' 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 8
오류 C2334 '{' 앞에 예기치 않은 토큰이 있습니다. 명백한 함수 본문을 건너뜁니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 8
오류 C3646 'getTel': 알 수 없는 재정의 지정자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 9
오류 C2059 구문 오류: '(' 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 9
오류 C2334 '{' 앞에 예기치 않은 토큰이 있습니다. 명백한 함수 본문을 건너뜁니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 9
오류 C2061 구문 오류: 식별자 'string' 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 10
오류 C2065 'string': 선언되지 않은 식별자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.cpp 2
오류 C2146 구문 오류: ')'이(가) 'n' 식별자 앞에 없습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.cpp 2
오류 C2143 구문 오류: ';'이(가) '{' 앞에 없습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.cpp 2
오류 C2447 '{': 함수 헤더가 없습니다. 이전 스타일의 형식 목록입니까? 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.cpp 2
오류 C3646 'name': 알 수 없는 재정의 지정자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 4
오류 C4430 형식 지정자가 없습니다. int로 가정합니다. 참고: C++에서는 기본 int를 지원하지 않습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 4
오류 C3646 'tel': 알 수 없는 재정의 지정자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 5
오류 C4430 형식 지정자가 없습니다. int로 가정합니다. 참고: C++에서는 기본 int를 지원하지 않습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 5
오류 C3646 'getName': 알 수 없는 재정의 지정자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 8
오류 C2059 구문 오류: '(' 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 8
오류 C2334 '{' 앞에 예기치 않은 토큰이 있습니다. 명백한 함수 본문을 건너뜁니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 8
오류 C3646 'getTel': 알 수 없는 재정의 지정자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 9
오류 C2059 구문 오류: '(' 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 9
오류 C2334 '{' 앞에 예기치 않은 토큰이 있습니다. 명백한 함수 본문을 건너뜁니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 9
오류 C2061 구문 오류: 식별자 'string' 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\Class.h 10
오류 C2011 'Person': 'class' 형식 재정의 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 5
오류 C2027 정의되지 않은 형식 'Person'을(를) 사용했습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 14
오류 C2065 'name': 선언되지 않은 식별자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 15
오류 C2065 'tel': 선언되지 않은 식별자입니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 16
오류 C2027 정의되지 않은 형식 'Person'을(를) 사용했습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 21
오류 C2027 정의되지 않은 형식 'Person'을(를) 사용했습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 26
오류 C2027 정의되지 않은 형식 'Person'을(를) 사용했습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 30
오류 C2027 정의되지 않은 형식 'Person'을(를) 사용했습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 36
경고 C4150 불완전한 형식 'Person'에 대한 포인터를 삭제했습니다. 소멸자가 호출되지 않습니다. 혼자 연습 C:\Users\minmi\source\repos\혼자 연습\혼자 연습\main.cpp 40

이만큼이나 나오네요. 근데 Class.h랑 Class.cpp의 코드를 main.cpp에 넣으면 정상작동이 되더라고요

코드를 합치면 잘되는데 분리했을때 안되는 이유가 뭐죠?

세벌의 이미지

extern 에 대해 공부해보시면 될 거 같아요.

익명 사용자의 이미지

재미있는 퀴즈.

#include<string>
string a; // <-- (1)
using namespace std;
string b; // <-- (2)

(1)의 string과 (2)의 string은 같을까요, 다를까요?

대충 보면 같아 보입니다. 하지만 같지 않습니다. 결정적으로, (1) 때문에 컴파일이 실패합니다.

using-directive는 자신이 나타난 뒤부터 그 효력이 발생합니다. 순서가 정말 중요하다는 말씀이지요.

minmin999333의 이미지

답변을 보고 using-directive 위치를 바꿨지만 여전히 컴파일은 그대로입니다 ㅠㅠ 어떻게 하죠...

익명 사용자의 이미지

당연히 한번엔 안 될 수도 있겠죠.

문제 하나를 고쳤으니 컴파일러 에러 메시지 중에 해결된 것이 있을 것이고, 그대로 남아있는 것이나 혹은 심지어 새로 나타난 에러 메시지도 있을 겁니다.

그러면 그걸 보고 진단을 해서 또 다른 문제를 찾아 고치는 것이죠.

프로그래밍이란 그런 겁니다. 그저 단순히 되면 되고 안 되면 안 되는 게 아니라, 안 되면 안 되는 이유가 있을 테니 그걸 찾아서 계속 고쳐나가는 과정이죠. 그 과정에서 실수도 하고 경험도 쌓고요.

그저 안 된다고 "컴파일러가 뭐라뭐라 하면서 안되요" 이러면서 누군가 도깨비 방망이라도 휘둘러서 순식간에 되게 만들어주길 바라고 있다면 발전이 전혀 없을겁니다.

댓글 달기

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