一千萬個為什麽

搜索

多個.cpp文件中包含的頭文件

我按照下面的一個簡單示例。但是編譯失敗了。

你能看看並給我一些建議。

使用了vs2010控制臺應用程序。謝謝。

錯誤消息

pgm.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
pgm.h(11): error C2143: syntax error : missing ',' before '&'
1>  multi_fct.cpp
pgm.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
pgm.h(11): error C2143: syntax error : missing ',' before '&'
1>  ch3_p96.cpp
pgm.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
pgm.h(11): error C2143: syntax error : missing ',' before '&'
ch3_p96.cpp(24): error C2664: 'prn_info' : cannot convert parameter 1 from 'const char [8]' to 'const int'
1>          There is no context in which this conversion is possible
ch3_p96.cpp(26): error C3861: 'fct1': identifier not found

pgm.h </強>

#include 
#include 
#include 

#define     N   3

void fctl(int k);
void fct2();
void prn_info(const string& pgm_name);

ch3_p96.cpp </強>

#include "stdafx.h"

#include 

#include "pgm.h"

using namespace std;

int main()
{
    char ans;
    int  k, n = N;

    cout << "This program does not do very much.\n";
    cout << "Do you want more information?\n";
    cout << "Enter either y or Y is yes.\n" << endl;

    cin >> ans;

    if (ans == 'y' || ans == 'Y')
        prn_info("ch3_p96");   //multi_main
    for (k = 0; k < n; ++k)
        fct1(k);

    cout << "Best Regards!" << endl;

}

multi_fct.cpp </強>

#include "StdAfx.h"

#include 
#include "pgm.h"

using namespace std;

void fct1(int n)
{
    int i;

    cout << "Hello from fct1()" << endl;
    for (i = 0; i < n; ++i)
        fct2();
}

void fct2()
{
    cout << "Hello from fct2()" << endl;

}

multi_prn.cpp </強>

#include "StdAfx.h"

#include 
#include 
#include "pgm.h"

using namespace std;

void prn_info(const string& pgm_name)
{
    cout << "Usage:  " << pgm_name << endl;
    cout << "This program illustrates a   " << endl;
    cout << "Program is more than one file.    " << endl;
    cout << "In this example, a single" << endl;
    cout << ".h file is included at the" << endl;
    cout << "top of our three .cpp files." << endl;
    cout << "Thus pgm.h acts as the \"glue\"" << endl;
    cout << "that binds the program." << endl;
}

最佳答案

您需要命名空間限定字符串: std :: string

編譯器錯誤說它沒有找到類型名稱,所以它猜測 stringint ,並繼續嘗試解析你的代碼。

此外,在一個地方你使用 fctl (fct lower-case-L),在一個地方你使用 fct1 (fct one)。

轉載註明原文: 多個.cpp文件中包含的頭文件