【问题标题】:Can't access function from header file无法从头文件访问函数
【发布时间】:2011-10-08 05:36:13
【问题描述】:
//head.h//
extern int sum(int,int);
//head.cpp//

#include "head.h"
#include "stdafx.h"
int sum(int x, int y)
{
return (x+y);
}
//mainfn.cpp//

#include "head.h"
#include "stdafx.h"
#include string
#include iostream
#include stdio.h
using std::string;
using std::cout;
using namespace System;

int main()
{
int x=10,y=2;
printf("value:  %d",sum(x,y));
Console::ReadLine();
return 0;
}

在 Visual Studio 2005 中构建时,此 vc++ 项目出现以下错误:

error C3861: 'sum': identifier not found.

谁能帮我解决这个问题?

【问题讨论】:

  • stdafx.h 中有什么,您的预编译头设置是什么?也许你对预编译的头文件有一些奇怪的事情,因为发布的代码没有问题。
  • 实际上当我在visual studio中启动新的VC++项目时,总是包含这个预编译的头文件。

标签: c++ visual-c++ stdafx.h


【解决方案1】:

您需要将包含 head.h 放在 stdafx.h 之后。启用预编译头文件后,编译器将忽略在包含 stdafx.h 之前(在这种情况下)发生的所有包含的内容。

【讨论】:

  • thnx chet.. 非常感谢纠正这个愚蠢的错误。现在好了。
  • @durgesh t:如果没问题 - 你可能想接受这个人的回答
【解决方案2】:

要么从项目中删除 stdafx.h,然后打开预编译的头文件.. 要么尝试将 head.h 移动到 stdafx.h 之后而不是之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2022-01-10
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多