CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2013
    Posts
    3

    Error C2065: 'variable' : undeclared identifier on line of decleration of variable

    Hello there,

    I am making a form application with a serial interface. I am getting the "Error C2065: '_serialPort' : undeclared identifier on line of decleration of _serialPort.

    How can the software complain that the identifier is not declared on the line where it is declared?

    I tried to google the thing, but couldn't get a clear answer.

    What is going on?

    Code:
    	private: String^ sendData(String^ data) {
    		try {
    			String^ message;
    
    			// Create a new SerialPort object with default settings.
    			_serialPort = gcnew SerialPort();
    
    			// Allow the user to set the appropriate properties.
    			_serialPort->PortName = comboBox1->Text;
    			_serialPort->BaudRate = 9600;
    			_serialPort->Parity = Parity::None;
    			_serialPort->DataBits = 8;
    			_serialPort->StopBits = StopBits::One;
    			_serialPort->Handshake = Handshake::None;
    
    			// Set the read/write timeouts
    			_serialPort->ReadTimeout = 500;
    			_serialPort->WriteTimeout = 500;
    
    			_serialPort->Open();
    
    			_serialPort->WriteLine(data);
    
    			message = _serialPort->ReadLine();
    			_serialPort->Close();
    		}
    		catch (Win32Exception^ ex) {
    			MessageBox::Show(ex->Message);
    		}
    		return message;
        }
    Code:
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	using namespace System::IO::Ports;
    Code:
    #include "stdafx.h"
    #include "Form1.h"
    #using <System.dll>
    #include "stdlib.h"

    Thanks.

    Kind regards,
    Marius

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Error C2065: 'variable' : undeclared identifier on line of decleration of variabl

    Wrong forum

  3. #3
    Join Date
    Aug 2013
    Posts
    3

    Re: Error C2065: 'variable' : undeclared identifier on line of decleration of variabl

    Please tell me how that could be. This is a VC++ question.

    Thank you for your time.

    Kind regards,
    Marius

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Error C2065: 'variable' : undeclared identifier on line of decleration of variabl

    Quote Originally Posted by king.oslo View Post
    Please tell me how that could be. This is a VC++ question.

    Thank you for your time.

    Kind regards,
    Marius
    It's a managed C++/CLI question.

  5. #5
    Join Date
    Aug 2013
    Posts
    3

    Re: Error C2065: 'variable' : undeclared identifier on line of decleration of variabl

    Ok. I am afraid I didn't know that. My apologies . I am sorry.

    How can we correct the mistake so that perhaps somebody will be able to help resolve the problem?Thanks.M
    Last edited by king.oslo; August 2nd, 2013 at 09:06 PM.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Error C2065: 'variable' : undeclared identifier on line of decleration of variabl

    Quote Originally Posted by king.oslo View Post
    Ok. I am afraid I didn't know that. My apologies . I am sorry.

    How can we correct the mistake so that perhaps somebody will be able to help resolve the problem?Thanks.M
    You could repost your question in the managed C++ forum. Somebody there can likely help.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Error C2065: 'variable' : undeclared identifier on line of decleration of variabl

    Quote Originally Posted by king.oslo View Post
    How can we correct the mistake so that perhaps somebody will be able to help resolve the problem?
    Compiler Error C2065
    Best regards,
    Igor

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Error C2065: 'variable' : undeclared identifier on line of decleration of variabl

    Quote Originally Posted by king.oslo View Post
    I am getting the "Error C2065: '_serialPort' : undeclared identifier on line of decleration of _serialPort.
    Code:
    // Create a new SerialPort object with default settings.
    _serialPort = gcnew SerialPort();
    Is that what you call declaration?
    It is not in C++ and not in managed C++.
    Try:
    Code:
    SerialPort _serialPort = gcnew SerialPort();
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Error C2065: 'variable' : undeclared identifier on line of decleration of variabl

    Moved the thread into the managed forum.

    With regard to the error...

    Code:
    private: String^ sendData(String^ data) {
    	try {
    		String^ message;
    
    		// Create a new SerialPort object with default settings.
    		_serialPort = gcnew SerialPort();

    The convention of prepending a camel cased variable with an underscore ('_') implies that the variable is a class field. So you need to define _serialPort as a class field.

    If you did not intend to make _serialPort a class variable, then drop the '_' and declare it inline.
    Code:
    // Create a new SerialPort object with default settings.
    var serialPort = gcnew SerialPort();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured