2016-01-17 24 views
-2
// Write a function named print_out that prints all the whole numbers from 1 to N. 
// by calling a function. 

#include "stdafx.h" 
#include <iostream> 
using namespace std; 

// Function must be declared before being used. 

void::print_out(int n); 

int main() { 
    int n; 

    cout << "Enter a number and press ENTER: "; 
    cin >> n; 

    print_out(n); 

    cout << endl; 
    return 0; 
} 

// Print-out function. 
// Prints numbers from 1 to n. 

int print_out(n) { 
    int i; 

    for (i = 1; i <= n; i++)  // For i = 1 to n, 
     cout << i << " ";  // print i 
    return n; 
} 

以下是錯誤:我宣佈N的功能,但它給我n是undentified錯誤後,我編寫的代碼,並嘗試運行

Error 1 error C2039: 'print_out' : is not a member of '`global namespace'' 
Error 2 error C2065: 'n' : undeclared identifier  
Error 3 error C2448: 'print_out' : function-style initializer appears to be a function definition 
    4 IntelliSense: identifier "n" is undefined 
    5 IntelliSense: identifier "n" is undefined 
+0

它應該是'INT print_out(INT N)',參數和在主可變是兩個獨立的實體。 – Borgleader

+0

它給我錯誤:不能重載由返回類型單獨區分的函數。當我放入for循環時。 – zezhawk22

回答

0

您應該聲明你的函數像這樣:

void print_out(int); //Takes an int argument, and 'void' specifies it has no return value 

它應該具有相同的返回類型作爲標題來定義:

void print_out(int n) { //is a 'void' type function as previously declared 
    for (int i = 1; i <= n; i++)  //We can declare 'i' in the for loop like this 
     cout << i << " ";  
    //Note we're not returning a value: It's a 'void' function. 
} 
+0

我修復它謝謝你。 – zezhawk22

0

這些都是簡單的語法錯誤:

// void::print_out(int n); //// :: is the scope operator. 
vpid print_out(int n); 

// int print_out(n) { //// the type must still be there. 
void print_out(int n) { 

修復這些,它編譯:http://coliru.stacked-crooked.com/a/e95fb196ad1fed2d

+0

但它必須是一個它要求我成爲的空白。它說print_out函數應該有void類型;它不會返回一個值。 – zezhawk22

+0

已編輯。簡單的修復。 – VermillionAzure

相關問題