错误C3861:'elbow':找不到标识符 [英] Error C3861: 'elbow': identifier not found
问题描述
您好,
我有一个问题,即我使用C ++创建了一个Windows窗体应用程序,并且有一个通过串口发送命令的程序。 Communicate.cpp是我的主要形式。我有Commands.cpp,它保存命令以发送串行端口。在Communicate.h中,我有一个调用函数的点击事件
elbow()但是每当我尝试构建它时,我都会收到错误:错误C3861:'elbow':标识符未找到I在Communicate.cpp中包含Commands.h,Commands.cpp中包含Commands.h。
Communicate.h函数调用:
Hi there,
I have an issue whereby I have created a Windows Forms application using C++ and have a program which sends commands through a serial port. Communicate.cpp is my main form. I have Commands.cpp which holds commands to send down the serial port. In Communicate.h I have a click event which calls function
"elbow()" however whenever I try to build it I get the error: "Error C3861: 'elbow': identifier not found" I have included Commands.h in Communicate.cpp and Commands.h is included in Commands.cpp.
Communicate.h function call:
private: System::Void testBtn_Click(System::Object^ sender, System::EventArgs^ e)
{
elbow(2, 1);
}
Communicate.cpp包括:
Communicate.cpp includes:
#include "Communicate.h"
#include "Commands.h"
Commands.cpp:
Commands.cpp:
#include "Commands.h"
#include "Communicate.h"
using namespace SerialCommunications;
using namespace System;
using namespace System::IO::Ports;
void elbow(int dir, int amt)
{
array<Byte>^ cmd = { 0xff, 0xff, 0xff };
//Communicate::serialPort1->Write(cmd, 0, 3);
for (int i = 0; i < 255; i++)
{
Communicate::serialPort1->Write(cmd, 0, 3);
//for (int i = 0; i < 1000; i++)
cmd[2]--;
}
return;
//Communicate::serialPort1->Write(cmd, 0, 3);
}
提前致谢,
Max
Thanks in advance,
Max
推荐答案
这里有两个问题。
- 一个是由C / C ++语言的语法引起的,它只能理解符号一个给定的位置,如果该符号在之前已经宣布那个位置。正如CPallini在他的解决方案中指出的那样,你必须在编译器可以找到它的地方放置函数elbow()
的声明,之前它遇到它的使用。
- 修复第一个问题后,第二个问题会以链接器错误的形式出现:通过实现函数标题
,并在至少两个编译单元中包含该标头,Communicate.h
中的elbowCommands.cpp
和Communcate.cpp
,你生成两个相同功能的组合,链接器将不知道使用哪一个!
第二个问题的解决方案是将函数testBtn_Click()
的实现移动到Communicate.cpp
。请注意,如果您在Commands.h
<内有elbow()
声明,这也将解决第一个问题br />
摘要:
1.确保声明elbow()
在Commands.h
2.将testBtn_Click()
的实现移动到Communicate.cpp
PS:你得到的错误的可能原因是编译器试图编译通信.h,它遇到#include Communicate.h,其中包含导致错误的实现。在那之后#include你也#included Commands.h,它可能有缺少的声明,编译器不能向前看代码,只能向后看。
There are two problems here.
- One is caused by the grammar of the C/C++ language, which does only understand symbols at a given position, if that symbol has been declared before that position. As CPallini has pointed out in his solution, you must put a declaration of the functionelbow()
somewhere where the compiler can find it, before it encounters its use.
- The second issue will hit you in the form of a linker error after you fix the first: by implementing the functionelbow
inside the headerCommunicate.h
, and including that header in at least two compilation units,Commands.cpp
andCommuncate.cpp
, you generate two compilates of the same function, and the linker will not know which one to use!
The solution for the second issue is to move the implementation of the functiontestBtn_Click()
intoCommunicate.cpp
. Note that this will also fix the first issue, if you have a declaration ofelbow()
insideCommands.h
Summary:
1. make sure there is a declaration ofelbow()
inCommands.h
2. Move the implementation oftestBtn_Click()
intoCommunicate.cpp
P.S.: the probable reason for the error you've got is that the compiler tries to compile Communicate.h, and there it encounters #include Communicate.h, which contains the implementation causing the error. While after that #include you've also #included Commands.h, which presumably has the missing declaration, the compiler can't look forward in code, only backward.
奇怪的是它可能看起来,你需要让Communicate.h
文件知道elbow
函数原型,因为函数调用是在这样的头文件中执行。
你应该把elbow
原型放在Communicate.h
(在testBtn_Click
方法之上)或Communicate.h
所包含的文件中(它也可以包括在内)Communicate.cpp
,但高于Communicate.h
包含)。
As strange as it may look, you need to make theCommunicate.h
file aware of theelbow
function prototype, because the function call is performed in such a header file.
You should putelbow
prototype either inCommunicate.h
(above thetestBtn_Click
method) or in a file included byCommunicate.h
(it could be included as well byCommunicate.cpp
, but aboveCommunicate.h
inclusion).
这篇关于错误C3861:'elbow':找不到标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!