多个 .cpp 文件中包含的头文件

发布于 2024-09-07 03:33:01 字数 2815 浏览 3 评论 0原文

我遵循了一个简单的例子,如下所示。但编译失败。

您能看一下并给我一些建议吗?

使用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 <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;
}

I followed a simple example as below. But compile failed.

Could you please have a look and give me some suggestion.

vs2010 console application used.Thank you.

Error message

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-09-14 03:33:01

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

编译器错误表示它没有找到类型名称,因此猜测 stringint,并继续尝试解析您的代码。

另外,在一个地方使用 fctl (fct 小写-L),在一个地方使用 fct1 (fct one)。

You need to namespace qualify string: std::string.

The compiler error is saying that it didn't find a type name, so it's guessing that string is an int, and continuing to try and parse your code.

Also, in one place you use fctl (fct lower-case-L) and in one place you use fct1 (fct one).

终止放荡 2024-09-14 03:33:01

您需要限定字符串:

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

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

You need to qualify string:

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

It is in the std namespace. Please do not use using namespace std in a header file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文