Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从编程珍珠中测量文本代码_C++ - Fatal编程技术网

C++ 从编程珍珠中测量文本代码

C++ 从编程珍珠中测量文本代码,c++,C++,我有来自编程珍珠的代码 #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,tex

我有来自编程珍珠的代码

#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;
}
#包括
//#包括
使用名称空间std;
模板
无效度量(字符*文本)
{
coutc:\users\david\documents\visualstudio 2010\projects\new\u practices\practices.cpp(22):错误C2143:语法错误:缺少“;”之前的“}”
1> c:\users\david\documents\visualstudio 2010\projects\new\u practices\practices.cpp(22):错误C2059:语法错误:'}'
1> c:\users\david\documents\visualstudio 2010\projects\new\u practices\practices.cpp(24):错误C2143:语法错误:缺少“;”在“{”之前
1> c:\users\david\documents\visualstudio 2010\projects\new\u practices\practices.cpp(24):错误C2447:“{”:缺少函数头(旧式正式列表?)
======生成:0成功,1失败,0最新,0跳过==========
我需要修正什么?

一些“错误”?谁写的程序都有NFI他们到底在做什么。该程序甚至不会产生任何有意义的输出……或者根本不会产生任何输出。你根本无法运行它或阅读源代码,获得相同的知识:零。

你需要添加“\”当您的定义跨越多行时,在行尾。 下面的代码编译时没有错误

 #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;
}
#包括“stdafx.h”
#包括
使用名称空间std;
模板
无效度量(字符*文本)
{

coutDeadMG是正确的,这里有很多错误。最突出的一个错误是您没有正确使用
#define
。它要求函数的所有代码都放在一行上

#define func(A, B) {//function body goes here}
要允许多行定义
s,请在行尾使用\s:

#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";\
}
什么是
t
?我想你的意思是
text
,而不是
t

下面的代码应该可以编译

#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;
}
#包括
#定义度量(T,文本){\

cout除了Stephen的答案之外,我可以说#define是un-C++ish。当您需要与字符串或类似内容相对应的变量名时,它会变得非常有用。对于大多数其他情况,请使用函数

除此之外,我必须同意DeadMG的观点,即至少我太蠢了,看不到编写此函数的任何理由。但这是另一个问题:)

如果您确实想要测量字符串的长度,可以使用函数

您的
度量值
似乎要打印出一系列新分配的11个内存地址。当您研究运行时的内存分配策略时,这很好,但在其他方面并不太有用

另外,不要忘记
delete
您拥有的
new
ed

#define MEASURE(T,text) {\
    cout<<text<<"\t";\
    cout<<sizeof(T)<<"\t";\
    ...
#定义度量(T,文本){\

我不能更改,但它有错误,甚至是什么“stdafx.h”都没有这样的目录stdafx是Microsoft用于加速预编译的标准头(如果我没记错的话)如果你不使用微软产品来编码你的C++,你既不需要也不需要它。只要跳过那一行。他几乎肯定地把T作为模板参数,而不是文本。没有任何意义。但是函数仍然毫无意义。我没有修复所有错误的主要原因是,即使你修复了所有错误,并为函数创建了一些输入,你仍然会得到垃圾和内存泄漏。啊,我没有想到。我必须承认,我对此代码修复的免责声明是,我几乎没有阅读实际的代码代码,我不想修复代码逻辑(我相信其他注释/答案中的代码逻辑已被严重破坏)。他问我如何修复编译错误,所以这就是我所帮助的-我将把关于代码的用途的讨论留给其他人!这很公平。这代码的含义并不是乞求别人看到。在将答案发布到StackOverflow时,你不应该使用粘贴箱。将更正后的代码作为答案的一部分发布。这不是吗be const T&text,以保持与#define相同的用法?不完全正确。如果参数作为字符串文本或字符[]提供:是的。const char*参数的工作方式不同。检查一下这段代码是否值得一本教科书:)如果你有const T&,那么T将被推断为char*,但也可以是std::string,等等。另一方面,如果你有const T*,那么你不能输入std::string,而在宏中,你可以。你指的是哪个“编程珍珠”?我假设这不是Jon Bentley的书“编程珍珠”-它不使用C++,AFAICR(至少在原始版本),不包括任何粗糙和un珍珠样的东西。
#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;
}
#define MEASURE(T,text) {\
    cout<<text<<"\t";\
    cout<<sizeof(T)<<"\t";\
    ...
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";
}