c++ testing code
here is code which print "c++" in binary form and at the same time measure time
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
unsigned int t = clock();
string c = "c++";
int t=0;
for (int i=0;i<c.length();i++){
t = (int) c[i];
while (t!=0){
cout << t % 2 <<endl;
t >>= 1;
}
}
unsigned int end = clock();
cout << end-t << endl;
return 0;
}
but here is mistake
1>------ Build started: Project: name_of_c++, Configuration: Debug Win32 ------
1> c++_name.cpp
1>c:\users\david\documents\visual studio 2010\projects\name_of_c++\c++_name.cpp(11): error C2371: 't' : redefinition; different basic types
1> c:\users\david\documents\visual studio 2010\projects\name_of_c++\c++_name.cpp(7) : see declaration of 't'
1>c:\users\david\documents\visual studio 2010\projects\name_of_c++\c++_name.cpp(12): warning C4018: '<' : sig开发者_运维百科ned/unsigned mismatch
1>c:\users\david\documents\visual studio 2010\projects\name_of_c++\c++_name.cpp(16): error C2059: syntax error : '>='
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Look at these two lines:
unsigned int t=clock();
int t=0;
These are both in the same scope, and both define a variable t
. This is not allowed in C++!
In case you are having trouble parsing the error message, when you get something like:
c++_name.cpp(11): error C2371: 't' : redefinition; different basic types
The number in parenthesis (11) tells you the line where the error occurred.
You know, compiler prints line numbers for errors.
Here is a problem:
unsigned int t=clock();
string c="c++";
int t=0;
First you declare t as unsigned int, and then you declare it as int.
you have declared t as unsigned int first here:
unsigned int t=clock();
and then defined again here as int
int t=0;
You can use a different variable name for the second one to get rid of this error.
精彩评论