This question already has answers here:
Is bool a native C type?
(12 answers)
Closed 6 years ago.
Why am I getting a syntax error for my C header declaration?
Here is my header file, viterbi.h:
#ifndef VITERBI_H
#define VITERBI_H
void vitdec(float* , int , int , bool* );
#endif //VITERBI_H
And here is my implementation file, viterbi.c:
// viterbi.c : Defines the entry point for the console application.
//
#include "viterbi.h"
#include "math.h"
//void vitdec(float* sd, int frameLen, int rate, bool* hd);
void vitdec(float* sd, int frameLen, int rate, bool* hd)
{
//... The rest of the function
The errors from the Visual Studio 2010 compiler read:
viterbi.h(4): error C2143: syntax error : missing ')' before '*'
viterbi.h(4): error C2081: 'bool' : name in formal parameter list illegal
viterbi.h(4): error C2143: syntax error : missing '{' before '*'
viterbi.h(4): error C2059: syntax error : ')'
viterbi.h(4): error C2059: syntax error : ';'
viterbi.c(7): error C2065: 'bool' : undeclared identifier
viterbi.c(7): error C2065: 'hd' : undeclared identifier
viterbi.c(7): warning C4552: '*' : operator has no effect; expected operator with side-effect
As far as I have seen/can tell, this is valid syntax for a C declaration. If I compile viterbi.c as C++ code (viterbi.cpp), then the errors disappear. What is the syntax error?
bool is not a native C type, but for those using C99, try adding the line #include <stdbool.h>, which contains a macro that defines bool.
Since the C compiler in all Visual Studio/MSVC products uses C89, bool is not defined at all for you, as a native C type or otherwise. Workarounds include using typedef or enum to define bool. Examples are in the below link.
For more information, see: Is bool a native C type?
Related
I'm trying to create a wrapper class for a C# dll and am coming across a compiler error in my definition. I've tried finding a solution and reading MSDN help but so far nothing works.
Error 2 error C2059: syntax error : 'public' 7
Error 3 error C2143: syntax error : missing ';' before '{' 8
Error 4 error C2447: '{' : missing function header (old-style formal list?) 8
Error 5 error C2059: syntax error : 'public' 7
Error 6 error C2143: syntax error : missing ';' before '{' 8
Error 7 error C2447: '{' : missing function header (old-style formal list?) 8
ASComms.h
#pragma once
#include "IPADDRESS.h"
namespace DataComms
{
public ref class CASComms
{
public:
void Connect(IPADDRESS ipaddr);
void Write(const char* plcTAG, const char* data);
private:
AutomatedSolutions::Win::Comm::AB::Logix::Net::Channel ^m_channel;
AutomatedSolutions::Win::Comm::AB::Logix::Device ^m_device;
AutomatedSolutions::Win::Comm::AB::Logix::Group ^m_group;
AutomatedSolutions::Win::Comm::AB::Logix::Item ^m_item;
};
}
ASComms.cpp
#include "stdafx.h"
#include "ASComms.h"
#include "IPADDRESS.h"
using namespace AutomatedSolutions::Win::Comm::AB::Logix;
void DataComms::CASComms::Connect(IPADDRESS ipaddr)
{
// do stuff
}
void DataComms::CASComms::Write(const char* plcTAG, const char* data)
{
// do stuff
}
I am making a DLL file for delphi applicaton in visual studio. I am following http://rvelthuis.de/articles/articles-cppobjs.html link to make the dll.My code is as follows
DLL_CLASS.h
#pragma once
using namespace std ;
#include "stdafx.h"
class DLL_CLASS
{
private:
public:
int TestValue;
char* getConnectedInverters (char* path);
char* getInverterParameters (int Device_Handle);
char* getInverter_SPOT_READINGS(int Device_Handle);
char* getPassword_varification();
char* get_Inverter_password();
char* get_Device_name(int Device_handle);
int get_Device_handel();
char* setAllInverterParameters(int Device_Handle,char* path_to_XML);
char* setYasdi_Reset_and_Shutdown();
char* get_Encryption(char* string_to_encrypt);
char* deviceXmlname();
char* RegExp(string Device_type);
int Get_inst_code_Channel(int Device_Handle);
char* set_One_InverterParameters(int Device_Handle,int Channel_handle,string Value);
DLL_CLASS();
virtual ~DLL_CLASS();
};
and other file code is as below
#include "stdafx.h"
#include "DLL_CLASS.h"
#include <tchar.h>
#include <windows.h>;
// define a macro for the calling convention and export type
#define EXPORTCALL __declspec(dllexport) __stdcall
typedef DLL_CLASS *ConsoleHandle;
extern "C"
{
ConsoleHandle EXPORTCALL NewConsole(void)
{
return new DLL_CLASS();
}
void EXPORTCALL DeleteConsole(ConsoleHandle handle)
{
delete handle;
}
char* EXPORTCALL NEW_getConnectedInverters(ConsoleHandle handle ,char* path)
{
handle->getConnectedInverters (path);
}
char* EXPORTCALL NEW_getInverterParameters (ConsoleHandle handle ,int Device_Handle)
{
handle->getInverterParameters(Device_Handle);
}
char* EXPORTCALL NEW_getInverter_SPOT_READINGS(ConsoleHandle handle,
int Device_Handle)
{
handle->getInverter_SPOT_READINGS(Device_Handle);
}
char* EXPORTCALL NEW_getPassword_varification(ConsoleHandle handle)
{
handle->getPassword_varification();
}
char* EXPORTCALL NEW_get_Inverter_password(ConsoleHandle handle)
{
handle->get_Inverter_password();
}
char* EXPORTCALL NEW_get_Device_name(ConsoleHandle handle,
int Device_handle)
{
handle->get_Device_name(Device_handle);
}
int EXPORTCALL NEW_get_Device_handel(ConsoleHandle handle)
{
return handle->get_Device_handel();
}
char* EXPORTCALL NEW_setAllInverterParameters(ConsoleHandle handle,int Device_Handle,char* path_to_XML)
{
return handle->setAllInverterParameters( Device_Handle, path_to_XML);
}
char* EXPORTCALL NEW_setYasdi_Reset_and_Shutdown(ConsoleHandle handle)
{
handle->setYasdi_Reset_and_Shutdown();
}
char* EXPORTCALL NEW_get_Encryption(ConsoleHandle handle,
char* string_to_encrypt)
{
handle->get_Encryption(string_to_encrypt);
}
char* EXPORTCALL NEW_deviceXmlname(ConsoleHandle handle)
{
handle->deviceXmlname();
}
char* EXPORTCALL RegExp(ConsoleHandle handle,
string Device_type)
{
handle->RegExp( Device_type);
}
char* EXPORTCALL NEW_set_One_InverterParameters(ConsoleHandle handle,
int Device_Handle,int Channel_handle,string Value)
{
handle->set_One_InverterParameters( Device_Handle,Channel_handle, Value);
}
int EXPORTCALL NEW_Get_inst_code_Channel(ConsoleHandle handle,
int Device_Handle)
{
handle->Get_inst_code_Channel(Device_Handle);
}
} // extern "C"
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
But this Give me error as below
1>------ Build started: Project: inverter_library, Configuration: Release Win32 ------
1> flatten.cpp
1>flatten.cpp(5): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>flatten.cpp(26): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(27): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(27): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(31): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(32): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(32): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(36): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(38): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(38): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(42): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(43): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(43): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(47): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(48): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(48): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(52): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(54): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(54): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(63): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(64): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(64): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(68): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(69): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(69): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(73): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(75): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(75): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(78): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(79): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(79): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(82): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(84): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(84): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(87): error C2059: syntax error : '__declspec(dllexport)'
1>flatten.cpp(89): error C2143: syntax error : missing ';' before '{'
1>flatten.cpp(89): error C2447: '{' : missing function header (old-style formal list?)
1>flatten.cpp(99): warning C4068: unknown pragma
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Can anyone suggest me Why this error is coming
I tried to find the solution but I could not find it....
You need to place the __declspec(dllexport) before the return value, and the __stdcall after the return value. For example:
__declspec(dllexport) int __stdcall foo(...)
Your macro places them together, after the function return value. Hence the syntax error. So if you want both in a single macro, you need a parameterized macro that takes the return type as a parameter.
It's simpler and clear to be explicit and skip the macros and write it out longhand, in my view. I suggest that you shun macros unless they give significant benefit in comparison to non-macro alternatives. In this case I see no such benefit.
Note that some of your flattening functions are missing return statements. And you also need to be clear on the lifetime of the char* pointers that you return. I trust you are already on top of that.
Quite a while back I was playing around with C code for a POS device. I could develop and build in Visual Studio 2008. Then to test I could go into cygwin, make the code and load onto the device, or use the included simulator. Somewhere along the lines that virtual box was lost.
I want to start up again in dabbling with this device but am having an awful time trying to get the code to build in Visual Studio 2008 C++ Express.
I created a new project from existing code, set it to compile as C code and tried to build. It complained that it could not find system.h, so I added the directory in the include directories under tools.
Now it gives 408 errors. Example as shown below:
------ Build started: Project: TestHelloWorld, Configuration: Debug Win32 ------
Compiling...
sysutil.c
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(105) : error C2054: expected '(' to follow '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(105) : error C2085: 'disable_interrupt' : not in formal parameter list
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(106) : error C2082: redefinition of formal parameter '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(106) : error C2143: syntax error : missing ';' before 'type'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(106) : error C2085: 'enable_interrupt' : not in formal parameter list
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(107) : error C2085: 'call_app_func' : not in formal parameter list
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(108) : error C2082: redefinition of formal parameter '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(108) : error C2146: syntax error : missing ',' before identifier 'DWORD'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(108) : error C2146: syntax error : missing ',' before identifier 'os_switch_app'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(108) : error C2143: syntax error : missing ';' before '('
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(108) : error C2059: syntax error : ')'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(109) : error C2054: expected '(' to follow '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(109) : error C2085: 'app5_call' : not in formal parameter list
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(110) : error C2082: redefinition of formal parameter '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(110) : error C2146: syntax error : missing ',' before identifier 'DWORD'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(110) : error C2146: syntax error : missing ',' before identifier 'app6_call'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(110) : error C2143: syntax error : missing ';' before '('
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(110) : error C2059: syntax error : ')'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(111) : error C2054: expected '(' to follow '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(111) : error C2085: 'app7_call' : not in formal parameter list
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(112) : error C2082: redefinition of formal parameter '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(112) : error C2146: syntax error : missing ',' before identifier 'DWORD'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(112) : error C2146: syntax error : missing ',' before identifier 'app8_call'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(112) : error C2143: syntax error : missing ';' before '('
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(112) : error C2059: syntax error : ')'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(113) : error C2054: expected '(' to follow '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(113) : error C2085: 'app9_call' : not in formal parameter list
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(114) : error C2082: redefinition of formal parameter '__pcs'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(114) : error C2146: syntax error : missing ',' before identifier 'DWORD'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(114) : error C2146: syntax error : missing ',' before identifier 'app10_call'
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(114) : error C2143: syntax error : missing ';' before '('
c:\cygwin\home\creon_sdk_v2.3\program\lib\system.h(114) : error C2059: syntax error : ')'
Does anybody have any idea how I can get this working.
The code is from the sample code that comes with the SDK for the device.
Also the SDK comes with cygwin which is installed and I can make the sample program no problem, however I am more of a C# developer and so like my IDE. I had this working before but cannot remember how I got it working before.
Please, any suggestions would be appreciated.
Kind regards,
Neill
Edit - As request line 101 - 115 from system.h
#ifndef _SYSTEM_H_
#define _SYSTEM_H_
#include "common.h"
extern __pcs void disable_interrupt();
extern __pcs void enable_interrupt();
extern DWORD call_app_func(void * ptr,DWORD param1,DWORD param2,DWORD param3);
extern __pcs DWORD os_switch_app(BYTE app_id,DWORD param1,DWORD param2,DWORD param3);
extern __pcs DWORD app5_call(DWORD func_no,DWORD param1,DWORD param2, DWORD param3);
extern __pcs DWORD app6_call(DWORD func_no,DWORD param1,DWORD param2, DWORD param3);
extern __pcs DWORD app7_call(DWORD func_no,DWORD param1,DWORD param2, DWORD param3);
extern __pcs DWORD app8_call(DWORD func_no,DWORD param1,DWORD param2, DWORD param3);
extern __pcs DWORD app9_call(DWORD func_no,DWORD param1,DWORD param2, DWORD param3);
extern __pcs DWORD app10_call(DWORD func_no,DWORD param1,DWORD param2, DWORD param3);
Thanks!
This particular compiler error message of the form of error C2054: expected '(' to follow '__pcs' indicates that the compile is attempting to decode the line of source as if __pcs is the beginning of a function name and it is expecting parenthesis to follow the identifier because it is expecting a function parameter list.
Whenever I have seen this it is because there is a define missing which will define __pcs to some function behavior modifier. For instance in Windows you may have something that will specify the calling sequence (C standard versus Pascal) or another one will be for DLL linkage.
I suggest you do a search through your header files looking for a define for __pcs to see where it might be defined.
It really looks like there is some kind of a -D compiler directive to define __pcs or a header file that needs to be included to define __pcs.
Here is a sample header file include dependency graph for a Cygwin application that might be helpful.
I have some errors in my header file, which I don't know how to fix because I am fairly new to C++.
Here is the code of the header file:
#pragma once
typedef unsigned int uint;
class DCEncryption
{
public:
static char* manageData(char*, char*, uint);
private:
static int max(int, int);
static uint leftRotate(uint, int);
};
And here are the errors:
- dcencryption.h(12): error C2062: type 'int' unexpected
- dcencryption.h(12): error C2334: unexpected token(s) preceding ':'; skipping apparent function body
- dcencryption.h(12): error C2760: syntax error : expected '{' not ';'
- dcencryption.h(13): error C2144: syntax error : 'uint' should be preceded by '}'
- dcencryption.h(13): error C2143: syntax error : missing ')' before ';'
- dcencryption.h(13): error C2059: syntax error : ')'
- dcencryption.h(13): error C2143: syntax error : missing ';' before ')'
- dcencryption.h(13): error C2238: unexpected token(s) preceding ';'
You are probably on Windows and you have included windef.h directly or indirectly (through windows.h, maybe) from your main .cpp file before including the shown file.
It so happens that max is a macro defined in windef.h that does not expand nicely in your context.
This can quite easily happen on some other platforms as well.
I have this code from programming pearls
#include <iostream>
//#include <string>
using namespace std;
template <class T>
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(t)<<"\n";
}
#define MEASURE(T,text){
cout<<text<<"\t";
cout<<sizeof(T)<<"\t";
int lastp=0;
for (int i=0;i<11;i++){
T *p=new T;
int thisp=(int)p;
if (lastp!=0)
cout<<" "<<thisp-lastp;
lastp=thisp;
}
cout<<"n":
}
int main(){
return 0;
}
but there are some mistakes
1>------ Build started: Project: new_practises, Configuration: Debug Win32 ------
1> practises.cpp
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(11): error C2143: syntax error : missing ';' before '<<'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(12): error C2143: syntax error : missing ';' before '<<'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(12): error C2086: 'int cout' : redefinition
1> c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(11) : see declaration of 'cout'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2059: syntax error : 'for'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2143: syntax error : missing ';' before '<'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2143: syntax error : missing ';' before '++'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2086: 'int i' : redefinition
1> c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14) : see declaration of 'i'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(21): error C2143: syntax error : missing ';' before '<<'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(21): error C2086: 'int cout' : redefinition
1> c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(11) : see declaration of 'cout'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(22): error C2059: syntax error : '}'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(22): error C2143: syntax error : missing ';' before '}'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(22): error C2059: syntax error : '}'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(24): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(24): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What do I need to fix?
DeadMG is correct, there are quite a few mistakes here. The most prominent one is that you are not using #define correctly. It requires that all of the code of the function goes on one line.
#define func(A, B) {//function body goes here}
To allow multi-line #defines, use \s at the end of the lines:
#define MEASURE(T,text) {\
cout<<text<<"\t";\
cout<<sizeof(T)<<"\t";\
int lastp=0;\
for (int i=0;i<11;i++){\
T *p=new T;\
int thisp=(int)p;\
if (lastp!=0) cout<<" "<<thisp-lastp;\
lastp=thisp;\
}\
cout<<"n";\
}
(Note I have fixed a few typos here, including one : where a ; should be.)
The other big problem with your code is in the measure function:
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(t)<<"\n";
}
What is t? I assume you mean text, not t.
The following should compile okay.
#include <iostream>
#define MEASURE(T,text) {\
cout<<text<<"\t";\
cout<<sizeof(T)<<"\t";\
int lastp=0;\
for (int i=0;i<11;i++){\
T *p=new T;\
int thisp=(int)p;\
if (lastp!=0) cout<<" "<<thisp-lastp;\
lastp=thisp;\
}\
cout<<"n";\
}
using namespace std;
template <class T>
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(text)<<"\n";
}
int main() {
return 0;
}
As a final, not directly bug-related question to you - why are you using a #define like this? Why not simply write the #define code into the measure method? #defineis usually used to give a short name for a variable or to declare very small functions - and there is a vocal part of the community that thinks they shouldn't be used at all!
In addition to Stephen's answer, I can say that #define is un-C++ish. It becomes useful when you need a variable name that corresponds to a string or something the like. For mostly all other cases, use functions.
Next to that, I must concur with DeadMG's opinion that at least I'm too stupid to see any reason to write this function. But that's another concern :)
If you actually want to measure the length of a string, you can use the function strlen.
What your MEASURE seems to do is print out a series of 11 newly allocated memory addresses. Nice to do when you are studying the memory allocation strategy of your runtime, but otherwise not too useful.
Also don't forget to delete what you have newed.
#define MEASURE(T,text) {\
cout<<text<<"\t";\
cout<<sizeof(T)<<"\t";\
...
can be directly translated into a template function: way easier to write, and to debug.
template< typename T > // assuming you _need_ a variable type of arguments
void MEASURE( const T& text) {
cout<<text<<"\t";
cout<<sizeof(T)<<"\t";
// will write the size of e.g. one character. Maybe
// you should use strlen or the like...
int lastp=0;
for (int i=0;i<11;i++){
T *p=new T;
int thisp=(int)p;
if (lastp!=0) cout<<" "<<thisp-lastp;
lastp=thisp;
}
cout<<"n";
}
"some" mistakes? Whoever wrote that has NFI what on earth they're doing. The program doesn't even produce any meaningful output. ... or any output at all. You could literally not run it or read the source code and gain the same knowledge: zero.
You need to add "\" at the end of the line when your definition across multiple lines.
Below code is compiled with no errors.
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(t)<<"\n";
}
#define MEASURE(T,text) { \
cout<<text<<"\t"; \
cout<<sizeof(T)<<"\t"; \
int lastp=0; \
for (int i=0;i<11;i++){ \
T *p=new T; \
int thisp=(int)p; \
if (lastp!=0) \
cout<<" "<<thisp-lastp; \
lastp=thisp; \
} \
cout<<"n": \
}
int main(){
return 0;
}