tags:

views:

72

answers:

4

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?

A: 

"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.

DeadMG
A: 

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;
}
no i have changed but it has mistakes and even what is "stdafx.h"there is no such kind of directory
stdafx is a Microsoft standard header for speeding up precompilation (if I remember correctly). If you aren't using a Microsoft product to code your C++ you neither have nor need it. Just skip that line.
Stephen
Stephen is correct, sorry for the late reply
+1  A: 

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!

Stephen
He almost certainly meant T as the template parameter, not text. sizeof(char*) makes no sense. The function is still meaningless however. The main reason that I didn't fix all the bugs is because even if you fixed all the bugs, and created some input for the functions, you'd STILL get garbage and memory leaks back out.
DeadMG
Ah, I didn't think of that. I must admit, my disclaimer for this code fix is that I barely read the actual code, and I'm not looking to fix the code logic (which I trust from the other comments/answers is badly broken). He asked how to fix the compilation errors, so that's what I helped with - I'll leave the discussions of what the code is meant to do to others!
Stephen
@Stephen: Fair enough. It's not like this code's meaning is begging to be seen.
DeadMG
You should not use a pastebin when posting your answer to StackOverflow. Post the corrected code as part of the answer.
Ken Bloom
A: 

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";
}
xtofl
DeadMG
Not really. If the argument is provided as a string literal or char[]: yes. const char* arguments work differently. Heck this code is worth a text book :)
xtofl
DeadMG
That's right. It's more meaningfull. Edited for that.
xtofl