Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
private: System::Void testBtn_Click(System::Object^  sender, System::EventArgs^  e)
{
    elbow(2, 1);
}



Communicate.cpp includes:

C++
#include "Communicate.h"
#include "Commands.h"


Commands.cpp:

C++
#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);
}


Thanks in advance,
Max
Posted
Comments
Afzaal Ahmad Zeeshan 16-Jun-15 14:41pm    
Why not write the function in a header file?
[no name] 16-Jun-15 16:19pm    
I've tried that, I get the same error.
Afzaal Ahmad Zeeshan 16-Jun-15 16:46pm    
Perhaps, your header files and the C++ files are not merged in a perfect manner. That is why you are still getting the error.
PIEBALDconsult 16-Jun-15 15:38pm    
Yeah, you likely need a function prototype in a header file.
[no name] 16-Jun-15 16:19pm    
I do have a function prototype but that makes no difference.

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 function elbow() 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 function elbow inside the header Communicate.h, and including that header in at least two compilation units, Commands.cpp and Communcate.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 function testBtn_Click() into Communicate.cpp. Note that this will also fix the first issue, if you have a declaration of elbow() inside Commands.h

Summary:
1. make sure there is a declaration of elbow() in Commands.h
2. Move the implementation of testBtn_Click() into Communicate.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.
 
Share this answer
 
v2
As strange as it may look, you need to make the Communicate.h file aware of the elbow function prototype, because the function call is performed in such a header file.
You should put elbow prototype either in Communicate.h (above the testBtn_Click method) or in a file included by Communicate.h (it could be included as well by Communicate.cpp, but above Communicate.h inclusion).
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 16-Jun-15 16:45pm    
+5, that is the solution that OP needs to implement.
Stefan_Lang 17-Jun-15 8:16am    
Correct, but judging by the comments above I suspect the declaration is already there, and the issue is really caused by the implementation being inside the header. Therefore the obvious fix, IMHO, would be to move the implementation into the cpp. Adding includes or declarations into a header where it doesn't belong doesn't seem right to me, unless, of course, if it's a template.
CPallini 17-Jun-15 8:32am    
Being an event handler, I suspect it is automatically generated by the IDE.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900