开发者

measure texts code from programming pearls

开发者 https://www.devze.com 2023-01-09 06:30 出处:网络
I have this code from programming pearls #include <iostream> //#include <string> using namespace std;

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&开发者_Python百科gt;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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消