473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Pointer problem (surprise, surprise) in Visual C++

Hi,

I am very new to C++ (i.e. only begun using it in the last few weeks) and am currently maintaining an existing .dll file for the company i work for. After reading quite a few threads in this forum i see that the main sticking point for many newbies appears to be pointers, and i am no exception. I want to really grasp the concept of pointers and could use a really good explanation (if you know of any particularly good websites, please feel free to post a link).

Now, my current question concerns a call to a function, passing a character array.....

Expand|Select|Wrap|Line Numbers
  1. ...
  2. ...
  3. ...
  4. char buffer1[10], buffer2[10];
  5.  
  6. GetDlgItemText(hWnd, IDC_C_LOW, buffer1, 10);
  7. GetDlgItemText(hWnd, IDC_C_UP, buffer2, 10);
  8.  
  9. if( !checkFloatInput(buffer1) || !checkFloatInput(buffer2) )
  10. {
  11.   EndDialog(hWnd, TRUE); // destroy the convexity dialog box
  12.   convexMin = 0;
  13.   convexMax = 0; // reset values
  14.  
  15.   // simulated left-click
  16.   Convex_LtClick(NULL, NULL, NULL, NULL, NULL, hWnd, NULL); 
  17.   break;
  18. }
  19. ...
  20. ...
  21. ...
  22.  



And the code for the checkFloatInput function is below:

Expand|Select|Wrap|Line Numbers
  1. BOOL checkFloatInput(char aCharArray[]) //used to error-trap non-float data entered by the user
  2. {
  3.  
  4.     int tempAsciiValue;
  5.     bool result = true;
  6.     int numOfDecimalPts = 0; //
  7.     char errorMsg[60] = "Invalid input:\n\n";
  8.  
  9.     for( int i = 0; i < int(strlen(aCharArray)); i++ )
  10.     {
  11.       tempAsciiValue = int(aCharArray[i]); // ascii value for character being considered
  12.  
  13.       if( tempAsciiValue==46 || ( tempAsciiValue > 47 && tempAsciiValue < 58) )
  14.       {
  15.         if( tempAsciiValue==46 && numOfDecimalPts > 0 )
  16.         {
  17.             i = strlen(aCharArray); // force end of for-loop
  18.             strcat(errorMsg, " - Cannot have more than one decimal point.\n\nPlease re-enter.");
  19.             MessageBox(NULL,(LPCSTR)errorMsg, NULL, MB_OK);
  20.             result = false;
  21.         }
  22.         else if( tempAsciiValue==46 && numOfDecimalPts == 0 )
  23.         {
  24.             numOfDecimalPts++; // 
  25.             aCharArray++; // move to next character in the array
  26.         }
  27.         else
  28.             aCharArray++; // move to next character in the array
  29.       }
  30.       else
  31.       {
  32.           i = strlen(aCharArray); // force end of for-loop                    
  33.           strcat(errorMsg, " - Non-numeric characters entered.\n\nPlease re-enter.");
  34.           MessageBox(NULL,(LPCSTR)errorMsg, NULL, MB_OK);
  35.           result = false;
  36.       }
  37.  
  38.     }// end of for-loop
  39.  
  40.     return result;
  41.  
  42. } // end of checkFloatInput function


The error message i get when compiling is as below:-

error C3861: 'checkFloatInput': identifier not found
error C3861: 'checkFloatInput': identifier not found
error C2365: 'checkFloatInput' : redefinition; previous definition was 'formerly unknown identifier'


The problem appears to be the parameter i am passing in the if-statement. I have tried many permutations of how the parameter is passed and how it is received in the function, but frustratingly, i am always getting compile errors. I am sure the answer is very simple and it is just my lack of experience with pointers that is the problem - I am hastily reading up on the subject, but i am also trying to learn Matlab and OriginPro at the same time, and any help would be much appreciated.

Thanks,

Phil
Dec 6 '07 #1
2 1603
oler1s
671 Expert 512MB
http://msdn2.microsoft.com/en-us/lib...yx(vs.71).aspx
Tip: Google can come in especially handy. So will MSDN when using Visual C++ and using WinAPI calls.

Diagnosis: Problem isn't the code within your function. The function itself is being called incorrectly. You have to pass in precisely the right number of parameters, and the correct type for each argument to the function when it is called.
Dec 6 '07 #2
Tip: Google can come in especially handy. So will MSDN when using Visual C++ and using WinAPI calls.

Diagnosis: Problem isn't the code within your function. The function itself is being called incorrectly. You have to pass in precisely the right number of parameters, and the correct type for each argument to the function when it is called.
Hi, thanks for getting back to me. I had already checked MSDN and had seen the page you recommended, but it still isn't quite 'clicking' in my mind yet. From MSDN, i understand that the GetDlgItemText(HWND hDlg, int nIDDlgItem, LPTSTR lpString, int nMaxCount) function takes the user's input and puts the string into a buffer pointed to by lpString.

I did some more reading (a pretty good intro to pointers in C can be found here for any other newbie's out there) around this area and amended my calling code as below:-

Expand|Select|Wrap|Line Numbers
  1. char* buffer1 = "";
  2. char* buffer2 = "";
  3.  
  4. UINT sizeBuffer1 = GetDlgItemText(hWnd, IDC_C_LOW, buffer1, 10);
  5. UINT sizeBuffer2 = GetDlgItemText(hWnd, IDC_C_UP, buffer2, 10);
  6.  
  7. // Note, that buffer1 and buffer2 are pointers to buffers where text is stored.
  8.  
  9. if( !checkFloatInput(buffer1, sizeBuffer1) || !checkFloatInput(buffer2, sizeBuffer2) )
  10. {
  11.    EndDialog(hWnd, TRUE); // destroy the convexity dialog box
  12.   ....
  13. ....
  14. ....
  15.  
I also amended my function a little, as below:-

Expand|Select|Wrap|Line Numbers
  1. BOOL checkFloatInput(char * aCharArray, UINT sizeOfArray) //used to error-trap non-float data entered by the user
  2. {
  3.  
  4.   int tempAsciiValue;
  5.   bool result = true;
  6.   int numOfDecimalPts = 0; //
  7.   char errorMsg[60] = "Invalid input:\n\n";
  8.  
  9.   for( int i = 0; i < (int)sizeOfArray; i++ )
  10.   {
  11.     tempAsciiValue = int(aCharArray); // ascii value for character being considered
  12.  
  13.     if( tempAsciiValue==46 || ( tempAsciiValue > 47 && tempAsciiValue < 58) )
  14.     {
  15.         if( tempAsciiValue==46 && numOfDecimalPts > 0 )
  16.         {
  17.     i = (int)sizeOfArray; // force end of for-loop
  18.     strcat(errorMsg, " - Cannot have more than one decimal point.\n\nPlease re-enter.");
  19.     MessageBox(NULL,(LPCSTR)errorMsg, NULL, MB_OK);
  20.     result = false;
  21.         }
  22.         else if( tempAsciiValue==46 && numOfDecimalPts == 0 )
  23.         {
  24.     numOfDecimalPts++; // 
  25.     aCharArray++; // move to next character in the array
  26.         }
  27.         else
  28.               aCharArray++; // move to next character in the array
  29.     }
  30.    else
  31.    {
  32.        i = (int)sizeOfArray; // force end of for-loop                
  33.        strcat(errorMsg, " - Non-numeric characters entered.\n\nPlease re-enter.");
  34.        MessageBox(NULL,(LPCSTR)errorMsg, NULL, MB_OK);
  35.        result = false;
  36.     }
  37.  
  38.   }// end of for-loop
  39.  
  40.   return result;
  41.  
  42. } // end of checkFloatInput function
  43.  
  44.  
However, i am still getting the same compilation error message. My thinking at present is that, when i call checkFloatInput(buffer1, sizeBuffer1), buffer1 is a pointer to a char array and as the checkFloatInput() function is set up to take a char pointer as one of it's arguments, this should work - I have tried passing buffer1 as &(*buffer1[0]) also, as well as quite a few other variations but always get the same error, "error C3861: 'checkFloatInput': identifier not found". I have googled the issue of 'error C3861', 'passing char arrays' and variations on that theme but as i say, unfortunately something hasn't gone eureka in my mind yet and still need some advice if there is anything else you could recommend. I am sure that the problem is with an assumption i am making about pointer types that i haven't yet corrected.

Cheers for any further light you may be able to shed.

Phil
Dec 7 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Hilary Zhang | last post by:
Hi, I have a headache with my program, which is a real-time graphic program written in VC++ 6.0. I often get an error of "access violation" when run in release mode. But if I use debug mode, it...
0
by: Gonçalo Rodrigues | last post by:
Hi all, I'm working around with smart pointers mostly as a learning exercise: I have a single-rooted hierarchy with the root class Object. Every Object has a member field count, the number of...
5
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and...
16
by: Sathyaish | last post by:
I am expecting a VBA code module in one of the VBA apps, but much to my astonishment, I don't seem to find my way through it. It seems like I am looking at a fully compiled binary. I have an MDB...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
156
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a...
3
by: David Mathog | last post by:
I have a program for which this line: if(! lstrtol(&atoken,length-2,(long *) &(lclparams->pad)) || (lclparams->pad< 0)){ generates the warning below, but ONLY if the gcc compiler is at -O2 or...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
14
by: Brian | last post by:
Hello - I have been told that for extra efficiency we should always compile code with frame pointer ommited (-fomit-frame-pointer), also -pipe. What is the reason for this? Why does it make a...
41
by: simonl | last post by:
Hi, I've been given the job of sorting out a crash in our product for which we have the crash information and an avi of the event (which can't possibly match but more of that later...) (btw this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.