GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Oct-2007, 07:00
tnorton tnorton is offline
New Member
 
Join Date: Oct 2007
Posts: 4
tnorton is on a distinguished road

Error 5 error C2440: 'return' : cannot convert from...


c++ dll - return/convert to a string from a function that retunds char[]

Hi all,

Need a little help here.

I am trying to return the MAC of a codec using the following function but cant work out how to convert the Buffer from char[5] to char or other string type as the return value.
This will be used in a DLL in a VB app. (Basically because my knowledge of C is very low)

The following code returns this error
Error 5 error C2440: 'return' : cannot convert from 'char [5]' to 'char' j:\Indigo Vision\COM\VBserial.dll\IVserial.cpp 194
Its probably obvious but I can seem to convert it

Tim Norton
CPP / C++ / C Code:
char DisplayDeviceInfo( const VBDEVICEINFO *pDeviceInfo )
 
{
    VBMSRV_PLAYBACKINFO pbInfo;
    UINT nValue;
    int  nSize;
    char Buffer[5];
 
     sprintf(Buffer,
        "%02X:%02X:%02X:%02X:%02X:%02X\n",
        pDeviceInfo->macAddress.abMacAddr[ 0 ],
        pDeviceInfo->macAddress.abMacAddr[ 1 ],
        pDeviceInfo->macAddress.abMacAddr[ 2 ],
        pDeviceInfo->macAddress.abMacAddr[ 3 ],
        pDeviceInfo->macAddress.abMacAddr[ 4 ],
        pDeviceInfo->macAddress.abMacAddr[ 5 ]
        );
 
       return Buffer;
}
  #2  
Old 24-Oct-2007, 07:38
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 478
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Error 5 error C2440: 'return' : cannot convert from...


For starters, your function signature suggest that you wish to return a single char, not an array of char. Secondly, if you want to convert it to a string object, most strings will have a constructor that takes a char* (or char[]). Try
CPP / C++ / C Code:
return string(Buffer);
  #3  
Old 24-Oct-2007, 07:47
tnorton tnorton is offline
New Member
 
Join Date: Oct 2007
Posts: 4
tnorton is on a distinguished road

Re: Error 5 error C2440: 'return' : cannot convert from...


I had alreay tried that as it seemed logical but get this error
Error 5 error C2440: 'return' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char *' j:\Indigo Vision\COM\VBserial.dll\IVserial.cpp 198


these are the includes I have. Am I missing one?

CPP / C++ / C Code:
#include "StdAfx.h"
#include "IVserial.h"
#include <stdio.h>
#include <atlstr.h>

#include <string>   //just for testing
#include <cstring> //just for testing
using std::string;


CPP / C++ / C Code:
char* DisplayDeviceInfo( const VBDEVICEINFO *pDeviceInfo )
{
    VBMSRV_PLAYBACKINFO pbInfo;
	char Buffer[5];

    sprintf(Buffer,
        "%02X:%02X:%02X:%02X:%02X:%02X\n",
        pDeviceInfo->macAddress.abMacAddr[ 0 ],
        pDeviceInfo->macAddress.abMacAddr[ 1 ],
        pDeviceInfo->macAddress.abMacAddr[ 2 ],
        pDeviceInfo->macAddress.abMacAddr[ 3 ],
        pDeviceInfo->macAddress.abMacAddr[ 4 ],
        pDeviceInfo->macAddress.abMacAddr[ 5 ]
        );
	return string(Buffer);

}
  #4  
Old 24-Oct-2007, 07:50
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 478
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Error 5 error C2440: 'return' : cannot convert from...


You have to change your function to return a string, not a char*.
  #5  
Old 24-Oct-2007, 07:53
tnorton tnorton is offline
New Member
 
Join Date: Oct 2007
Posts: 4
tnorton is on a distinguished road

Re: Error 5 error C2440: 'return' : cannot convert from...


did that last night too but get this error, which I dont understand
Error 9 error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl DisplayDeviceInfo(struct tagVBDEVICEINFO const *)" (?DisplayDeviceInfo@@YA?AV?$basic_string@DU?$char_ traits@D@std@@V?$allocator@D@2@@std@@PBUtagVBDEVIC EINFO@@@Z) referenced in function "char __stdcall Nortronics::GetMAC(char *)" (?GetMAC@Nortronics@@YGDPAD@Z) IVserial.obj
  #6  
Old 24-Oct-2007, 08:08
tnorton tnorton is offline
New Member
 
Join Date: Oct 2007
Posts: 4
tnorton is on a distinguished road

Re: Error 5 error C2440: 'return' : cannot convert from...


OK, I feel like an idiot now.
The second error wasfor a differnet function
sorry
  #7  
Old 24-Oct-2007, 08:08
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 478
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Error 5 error C2440: 'return' : cannot convert from...


I can't really tell without looking at all of the code, but do the function signatures exactly match. In other words...
CPP / C++ / C Code:
// somewhere in the header
string DisplayDeviceInfo( const VBDEVICEINFO *pDeviceInfo );

// in the cpp file
string DisplayDeviceInfo( const VBDEVICEINFO *pDeviceInfo )
{
    VBMSRV_PLAYBACKINFO pbInfo;
	char Buffer[5];

    sprintf(Buffer,
        "%02X:%02X:%02X:%02X:%02X:%02X\n",
        pDeviceInfo->macAddress.abMacAddr[ 0 ],
        pDeviceInfo->macAddress.abMacAddr[ 1 ],
        pDeviceInfo->macAddress.abMacAddr[ 2 ],
        pDeviceInfo->macAddress.abMacAddr[ 3 ],
        pDeviceInfo->macAddress.abMacAddr[ 4 ],
        pDeviceInfo->macAddress.abMacAddr[ 5 ]
        );
	return string(Buffer);
}
If that doesn't solve the problem, you may want to post your header file as well as the cpp file and I'll give it a better look.
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Message Class TransformedBG C++ Forum 5 29-Nov-2006 21:28
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
Palindrome Without Library Functions? fito C Programming Language 2 30-Nov-2004 02:53
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.