2016-10-08 3 views
-4

그래서 여기에 내가 풀려고하는 원래의 질문이 있습니다.별도의 .cpp 파일에서 함수를 호출하는 방법은 무엇입니까?

* Lab5_Figures라는 프로젝트를 만듭니다. 이 프로젝트는 여러 파일을 포함해야합니다. 사용자에게 정사각형, 왼쪽 또는 오른쪽 삼각형 중 하나를 선택하라고 반복적으로 묻는 프로그램을 작성한 다음 그림 크기를 입력 한 다음 적절한 모양을 별 모양으로 인쇄합니다. 사각형의 경우, 프로그램은 사용자가 채워지거나 빈 사각형이 필요한지 여부를 묻습니다. 사용자가 잘못된 옵션을 입력하면 프로그램을 종료해야합니다.

  1. 평방
  2. 왼쪽 아래 삼각형
  3. 오른쪽 삼각형

선택 그림 : 1

선택 크기 : 4

작성 또는 중공 예를 들어 아래 대화보기 [f/h] : h

// 것은 다음

  1. 평방
  2. 왼쪽 아래 삼각형
  3. 오른쪽 삼각형 ...

당신은 반복 실험실에서 코드를 재사용 할 수 있습니다 (I 반복 적절한 그림을 인쇄 벌써 했어.) 별 인쇄 코드를 filledSquare, hollowSquare, leftTriangle, rightTriangle의 네 가지 기능으로 배치하십시오. 각 함수는 단일 정수 매개 변수를 받아 들여야합니다. 즉, Figure의 크기와 값을 반환하지 않습니다 (void 함수 임). figure.cpp, figures.h 및 figuresInput.cpp라는 세 개의 개별 파일을 만듭니다. Figures.cpp에 삼각형 및 사각형 함수 정의를 배치하고 figures.h에 프로토 타입을 배치합니다. 헤더 파일이 다중 포함되지 않도록 보호되어 있는지 확인하십시오. FiguresInput.cpp *에 main 함수를 추가하십시오.

좋습니다. 이제 내 파일들입니다.

이 figuresInput.cpp

// This program creates shapes based on user input 


#include <iostream> 
#include <string> 
#include "figures.h" 

using std::cin; using std::cout; using std::string; using std::endl; 

int main() 
{ 
    int option = 1; 

while (option == 1 || option == 2 || option == 3) 
{ 
    //determine choice 
    cout << "1. Square" << endl << "2. Left Triangle" << endl << "3. Right Triangle" << endl; 
    cout << "Select an option: "; 

    cin >> option; 

    if (option == 1) 
    { 
     char fillHollow; 

     cout << "Filled or hollow? [f/h]"; 
     cin >> fillHollow; 

     if (fillHollow == 'f') 
     { 
      int size; 

      cout << "Input Size: "; 
      cin >> size; 

      void filledSquare(int size); 
     } 

     else if (fillHollow = 'h') 
     { 
      int size; 

      cout << "Input Size: "; 
      cin >> size; 

      void hollowSquare(int size); 
     } 
    } 

    else if (option == 2) 
    { 
     int size; 

     cout << "Input Size: "; 
     cin >> size; 

     void leftTriangle(int size); 
    } 

    else if (option == 3) 
    { 
     int size; 

     cout << "Input Size: "; 
     cin >> size; 

     void rightTriangle(int size); 
    } 

    else 
     exit(EXIT_FAILURE); 
} 
} //end main 

figures.cpp

//function defintions 

#include "figures.h" 
#include <iostream> 

using std::cin; using std::cout; using std::string; using std::endl; 

void filledSquare(int a) 
{ 
//print stars for first square 
for (int b = 0; b < a; b++) 
{ 
    for (int c = 0; c < a; c++) 
     cout << "*"; 
    cout << endl; //new line 
} 
cout << endl; //new line 

} //end 

void hollowSquare(int a) 
{ 
for (int b = 0; b < a; b++) 
{ 
    int spaces = a - 2; 
    if (b == 0 || b == (a - 1)) 
    { 
     for (int i = 0; i < a; i++) 
      cout << "*"; 
     cout << endl; //new line 
    } 
    else 
    { 
     cout << "*"; 
     for (int i = 0; i < spaces; i++) 
      cout << " "; 
     cout << "*"; 
     cout << endl; //new line 
    } 
} 
} //end 

void leftTriangle(int a) 
{ 
//get user input and print stars for first triangle 
for (int b = a; b < a; b--) 
{ 
    for (int c = 0; c < b; c++) 
     cout << "*"; 
    cout << endl; //new line 
} 
cout << endl; //new line 
} //end 

void rightTriangle(int a) 
{ 
//get user input and print stars for second triangle 
for (int b = 0; b < a; b++) 
{ 
    int stars = a - b; 
    for (int i = 0; i < b; i++) 
     cout << " "; 
    for (int i = 0; i < stars; i++) 
     cout << "*"; 
    cout << endl; //new line 
} 
cout << endl; //new line 
} //end 
마지막

및 figures.h (내 포맷이 꺼져 있으면 나는 :(사과)

//funtion prototypes 

#ifndef FIGURES_H 
#define FIGURES_H 

void filledSquare(int); 

void hollowSquare(int); 

void leftTriangle(int); 

void rightTriangle(int); 

#endif; 

그래, 내 문제는 내가 main에서 함수 정의를 올바르게 호출하지 않는다고 생각한다. 내가 딱 맞는 것을 포함시키지 않았는지 나는 확신 할 수 없다. 내가 얻을 수있는 도움에 정말 감사 할 것입니다.

내 결과는 다음과 같습니다.

1. Square 
2. Left Triangle 
3. Right Triangle 
Select an option: 1 
Filled or hollow? [f/h]f 
Input Size: 4 
1. Square 
2. Left Triangle 
3. Right Triangle 
Select an option: 
+0

[MCVE]에 축 어적 오류 메시지를 넣어주세요. –

+0

문제가 무엇인지 잘 모르겠습니다. 함수는 오류나 경고없이 컴파일하고 모든 것을 잘 인쇄합니다. 루프가 올바르게 반복되어 입력을 올바르게 선택합니다. 그러나 결코 모양을 인쇄하지 않습니다. – Inert

+0

이러한 문제를 해결하는 올바른 도구는 디버거입니다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –

답변

0

을하지만 당신은 같은 서명 및 RETURN TYPE과만큼 다시 선언됩니다

는이처럼 그들을 호출합니다. 이 시나리오에서 컴파일러는 예를 들어 불평하지 않습니다

void foo(); // ok 
void foo(); // ok 
void foo(); // ok 

모든 프로토 타입은 상기와 그들이 모든 일에 동일하지만 당신은 그들이 동일한 기능의 프로토 타입 만 있기 때문에 하나의 정의를 제공해야하기 때문에 컴파일러에 정확합니다.

void foo(); // ok 
    foo(); // error foo differs only in return type which doesn't mean overloading it. this function without return type by default returns int 
char foo(); // error foo differs only in return type. 

위의 프로토 타입은 반환 유형이 다르므로 올바르지 않습니다. 함수 시그니처의 오버로드는 매개 변수의 수나 매개 변수의 유형 (적어도 하나의 매개 변수가 다를 경우) 또는 두 가지에서 달라야합니다.

void foo();  // ok first prototype with which the compiler compare the following 
int foo(int);  // ok differs in number and type of parameters 
    foo(int&)  // ok differs in number and type of parameters 
void foo(int, int) // ok differs in number and type of parameters 

int 예를 들어, 이미 선언 된 경우 fillSquare를 다시 선언해야합니다.

void filledSquare(int size); // ??!! here you are not calling this function but you are declaring it instead. 

함수 그냥 어떤 필요하지만 매개 변수의 유형을 명시 적이있는 경우 매개 변수를 넣어하는 내부 반환 형식 및 괄호없이 함수의 이름을 넣어 불러옵니다.

filledSquare(size); // without return type nor type of parameters 
+0

그것이 도움이된다면 대답을 받아들입니다. – Raindrop7

2

실제로 함수를 호출하지는 않지만 선언 할 수 있습니다.당신이 filledSquare 함수를 호출하지 않는 당신의 예에서

hollowSquare(size); 
+0

감사합니다. 나는 네가 아는 것 이상으로 감사한다! 나는 그것이 바보 같았다는 것을 알았습니다. 모두 그것이 논리 오류라고 생각했습니다 : P – Inert

관련 문제