2010-06-25 107 views
0

我遵循了一个简单的例子,如下所示。但编译失败。包含在多个.cpp文件中的头文件

请你看看,给我一些建议。

vs2010控制台应用程序used.Thank你。

错误消息

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 <iostream> 
#include <cstdlib> 
#include <string> 

#define  N 3 

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

ch3_p96.cpp

#include "stdafx.h" 

#include <iostream> 

#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 <iostream> 
#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 <iostream> 
#include <string> 
#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; 
} 
+2

您的错误列出第11行,但您只包含头文件中的9行.... – 2010-06-25 02:17:31

+0

我从第3行写入#include 。保留第1行和第2行为空。 – 2010-06-25 02:19:04

回答

3

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

编译器错误是说它没有找到类型名称,所以它猜测stringint,并继续尝试和解析您的代码。

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

+0

非常感谢。我需要更多细节导向。像'1'和'l'。 – 2010-06-25 02:30:43

+0

@Nano:责怪字体:) – Stephen 2010-06-25 02:35:31

1

需要判定字符串:

void prn_info(const std::string& pgm_name); 

它在std命名空间。请不要在头文件中使用using namespace std

+0

'ch3_p96.cpp(26):错误C3861:'fct1':标识符找不到.'唯一的一个编译器错误依然存在。谢谢。 – 2010-06-25 02:22:48

+1

@Nano:应该是一个小写的L而不是一个。 – Stephen 2010-06-25 02:27:56