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: '<' : signed/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.
Related
I am writing an interface program and I am using classes to write the interface as well as some of the other options within the program. This is what I have written so far:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <iostream>
#include <vector>
#include <string>
class Interface
{
private:
typedef vector<string> programType;
programType programCode;
public:
void startInterface()
{
char q, input;
do
{
cout << "PySUB Interpreter 1.0 on Windows (September 2020)" << endl;
cout << "Enter program lines or read(<filename>.py) at command line interface" << endl;
cout << "Type 'help' for more information or 'quit' to exit" << endl;
} while (input != q);
}
void quit();
void help();
void show();
void clear();
};
#endif
The errors that I am getting are:
Build started...
1>------ Build started: Project: pysub1, Configuration: Debug x64 ------
1>interface.cpp
1>C:\Users\Johnathon\source\repos\pysub1\pysub1\interface.h(11,16): error C2143: syntax error: missing ';' before '<'
1>C:\Users\Johnathon\source\repos\pysub1\pysub1\interface.h(11,16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Users\Johnathon\source\repos\pysub1\pysub1\interface.h(11,36): error C2238: unexpected token(s) preceding ';'
1>C:\Users\Johnathon\source\repos\pysub1\pysub1\interface.h(12,14): error C3646: 'programCode': unknown override specifier
1>C:\Users\Johnathon\source\repos\pysub1\pysub1\interface.h(12,25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Users\Johnathon\source\repos\pysub1\pysub1\interface.h(21,72): error C2065: 'endl': undeclared identifier
1>C:\Users\Johnathon\source\repos\pysub1\pysub1\interface.h(22,91): error C2065: 'endl': undeclared identifier
1>C:\Users\Johnathon\source\repos\pysub1\pysub1\interface.h(23,73): error C2065: 'endl': undeclared identifier
1>Done building project "pysub1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
How do I resolve this?
The problem here is that you don't specify the namespace of cout, endl, etc. You should either change them them to std::cout and so on, or write using namespace std at the top of your file (which is generally considered bad practice, but will do just fine in your example).
Well, I successfully built the test programme:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
but when I tried the one for boost::mpl::map, it barfed:
#include <boost/mpl/map.hpp>
#include <boost/mpl/assert.hpp>
int main()
{
using std::is_same;
using boost::mpl::at;
using boost::mpl::long_;
using boost::mpl::void_;
using boost::mpl::map;
using boost::mpl::pair;
//using namespace boost::mpl;
typedef map <
pair<int, unsigned>
, pair<char, unsigned char>
, pair<long_<5>, char[17]>
, pair < int[42], bool >
> m;
BOOST_MPL_ASSERT_RELATION(size<m>::value, == , 4);
BOOST_MPL_ASSERT_NOT((empty<m>));
BOOST_MPL_ASSERT((is_same< at<m, int>::type, unsigned >));
BOOST_MPL_ASSERT((is_same< at<m, long_<5> >::type, char[17] >));
BOOST_MPL_ASSERT((is_same< at<m, int[42]>::type, bool >));
BOOST_MPL_ASSERT((is_same< at<m, long>::type, void_ >));
return 0;
}
This is the compiler output:
1>------ Build started: Project: example, Configuration: Debug Win32 ------
1> example.cpp
1>c:\projects\example\example\example.cpp(22): error C2027: use of undefined type 'boost::mpl::size<m>'
1>c:\projects\example\example\example.cpp(22): error C2065: 'value' : undeclared identifier
1>c:\projects\example\example\example.cpp(22): error C2975: 'x' : invalid template argument for 'boost::mpl::assert_relation', expected compile-time constant expression
1> c:\boost_1_57_0\boost\mpl\assert.hpp(120) : see declaration of 'x'
1>c:\projects\example\example\example.cpp(22): error C2664: 'int boost::mpl::assertion_failed<0>(boost::mpl::assert<false>::type)' : cannot convert argument 1 from 'boost::mpl::failed ************boost::mpl::assert_relation<0,4,bool boost::mpl::operator ==(boost::mpl::failed,boost::mpl::failed)>::* ***********' to 'boost::mpl::assert<false>::type'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\boost_1_57_0\boost\mpl\assert.hpp(176): error C2027: use of undefined type 'boost::mpl::empty<m>'
1> c:\projects\example\example\example.cpp(23) : see reference to class template instantiation 'boost::mpl::assert_arg_pred<boost::mpl::empty<m>>' being compiled
1>c:\boost_1_57_0\boost\mpl\assert.hpp(176): error C2146: syntax error : missing ';' before identifier 'p_type'
1>c:\boost_1_57_0\boost\mpl\assert.hpp(176): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost_1_57_0\boost\mpl\assert.hpp(177): error C2653: 'p_type' : is not a class or namespace name
1>c:\boost_1_57_0\boost\mpl\assert.hpp(177): error C2065: 'value' : undeclared identifier
1>c:\boost_1_57_0\boost\mpl\assert.hpp(177): error C2975: 'unnamed-parameter' : invalid template argument for 'boost::mpl::assert_arg_pred_impl', expected compile-time constant expression
1> c:\boost_1_57_0\boost\mpl\assert.hpp(171) : see declaration of 'unnamed-parameter'
1>c:\boost_1_57_0\boost\mpl\assert.hpp(182): error C2027: use of undefined type 'boost::mpl::empty<m>'
1> c:\projects\example\example\example.cpp(23) : see reference to class template instantiation 'boost::mpl::assert_arg_pred_not<boost::mpl::empty<m>>' being compiled
1>c:\boost_1_57_0\boost\mpl\assert.hpp(182): error C2146: syntax error : missing ';' before identifier 'p_type'
1>c:\boost_1_57_0\boost\mpl\assert.hpp(182): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost_1_57_0\boost\mpl\assert.hpp(183): error C2653: 'p_type' : is not a class or namespace name
1>c:\boost_1_57_0\boost\mpl\assert.hpp(183): error C2065: 'value' : undeclared identifier
1>c:\boost_1_57_0\boost\mpl\assert.hpp(183): error C2057: expected constant expression
1>c:\boost_1_57_0\boost\mpl\assert.hpp(184): error C2975: 'unnamed-parameter' : invalid template argument for 'boost::mpl::assert_arg_pred_impl', expected compile-time constant expression
1> c:\boost_1_57_0\boost\mpl\assert.hpp(171) : see declaration of 'unnamed-parameter'
1>c:\projects\example\example\example.cpp(23): error C2668: 'boost::mpl::assert_not_arg' : ambiguous call to overloaded function
1> c:\boost_1_57_0\boost\mpl\assert.hpp(203): could be 'boost::mpl::assert<false> boost::mpl::assert_not_arg<boost::mpl::empty<m>>(void (__cdecl *)(Pred),int)'
1> with
1> [
1> Pred=boost::mpl::empty<m>
1> ]
1> c:\boost_1_57_0\boost\mpl\assert.hpp(194): or 'boost::mpl::failed ************boost::mpl::not_<boost::mpl::empty<m>>::* ***********boost::mpl::assert_not_arg<boost::mpl::empty<m>>(void (__cdecl *)(Pred),int)'
1> with
1> [
1> Pred=boost::mpl::empty<m>
1> ]
1> while trying to match the argument list '(void (__cdecl *)(boost::mpl::empty<m>), int)'
1>c:\projects\example\example\example.cpp(25): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Even doing the following:
#include <boost/mpl/map.hpp>
int main()
{
using std::is_same;
using boost::mpl::at;
using boost::mpl::long_;
using boost::mpl::map;
using boost::mpl::pair;
typedef map <
pair<int, unsigned>
, pair<char, unsigned char>
, pair<long_<5>, char[17]>
, pair < int[42], bool >
> m;
at<m, int>::type a;
return 0;
}
fails:
1>------ Build started: Project: example, Configuration: Debug Win32 ------
1> example.cpp
1>c:\projects\example\example\example.cpp(18): error C2027: use of undefined type 'boost::mpl::at<m,int>'
1>c:\projects\example\example\example.cpp(18): error C2065: 'type' : undeclared identifier
1>c:\projects\example\example\example.cpp(18): error C2146: syntax error : missing ';' before identifier 'a'
1>c:\projects\example\example\example.cpp(18): error C2065: 'a' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I've installed VS Community 2013 Update 4. Am I using this incorrectly or is this a failure with the version I've installed? Release notes for 1.57.0 doesn't reference anything, nor does their bug system.
Basically there's a whole slew of missing headers. The last, simplest, one just misses
#include <boost/mpl/at.hpp>
#include <type_traits>
(that's on GCC, so MSVC might require some more due to implementation defined indirect header includes).
The full map sample needs at least
#include <boost/mpl/assert.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/size.hpp>
#include <type_traits>
And some more usings:
using boost::mpl::empty;
using boost::mpl::size;
Works:
int GlobalVar = 5;
int * LPVar[] = {&GlobalVar};
Doesn't work:
int GlobalVar = 5;
int * LPVar[]; // int * LPVar[1] doesn't work too
LPVar[0] = &GlobalVar; // errors here
------ Build started: Project: pointers, Configuration: Release Win32 ------ Compiling... ilovpointers.cpp .\ilovpointers.cpp(9) : error C2466: cannot allocate an array of constant size 0
.\ilovpointers.cpp(9) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int .\ilovpointers.cpp(9)
: error C2040: 'LPVar' : 'int []' differs in levels of indirection
from 'int *[1]' .\ilovpointers.cpp(9) : error C2440: 'initializing' :
cannot convert from 'int *' to 'int []'
There are no conversions to array types, although there are conversions to references or pointers to arrays Build log was saved at
"file://f:\Visual Studio
C++\Project1\pointers\pointers\Release\BuildLog.htm" pointers - 4
error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
EDIT:
I solved but got question.
#include <iostream>
#include <stdio.h>
using namespace std;
int GlobalVar = 5;
int * LPVar[1];
void main()
{
LPVar[0] = &GlobalVar;
printf("%i", *LPVar[0]);
cin.get();
}
LPVar is a global array so why it didn't work when I have put it just after definition?
Is the code at file scope?
The answer appears to be yes given the updated code fragment.
If so, you can't have random assignments like LPVar[0] = &GlobalVar; written at file scope. You can only have declarations (without initializers) or definitions (optionally with initializers) at file scope.
In your question, this works (as indeed it should), because there are two variable definitions with initializers.
int GlobalVar = 5;
int *LPVar[] = {&GlobalVar};
This code does not work (as indeed it shouldn't), because the third line is an assignment statement and not a declaration or definition:
int GlobalVar = 5;
int *LPVar[]; // Declaration, not definition
LPVar[0] = &GlobalVar; // Assignment is not allowed outside a function body
Variant:
int GlobalVar = 5;
int *LPVar[1]; // Definition without initializer
LPVar[0] = &GlobalVar; // Assignment is not allowed outside a function body
I have some troubles with the following code
#include<iostream>
#include<iomanip>
#include<fstream>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<functional>
#include<algorithm>
using namespace std;
struct node
{
int weight;
unsigned char value;
const node * child0;
const node *child1;
node(unsigned char c=0,int i=-1){
value=c;
weight=-1;
child0=0;
child1=0;
}
//construct new internal node that has children c1 and c2
node (const node* c0,const node *c1){
value=0;
weight=c0->weight+c1->weight;
child0=c0;
child1=c1;
}
bool operator<(const node &a) const {
return weight<a.weight;
}
void traverse(char * code=" " ) const;
};
void node::traverse(char * code ) const
{
if(child0)
{
child0->traverse(code +'0');
child1->traverse(code +'1');
}
else
{
cout<<" "<<value <<" ";
cout<<setw(2)<<weight;
cout<<" "<<code<<endl;
}
}
void count_chars(int *counts)
{
for (int i=0;i<256;i++)
counts[i]=0;
//ifstream file( "input.data");
//if(!file){
//cerr<<" couldnt open input file!\n";
//throw "abort";
//file.setf(ios::skipws);
unsigned char c;
while(true)
{
cin>>c;
if(c){
counts[c]++;
}
else
break;
}
}
int main()
{
int counts[256];
count_chars(counts);
priority_queue<vector<node>,greater<node> >q;
for(int i=0;i<256;++i)
if(counts[i])
q.push(node(i,counts[i]));
while(q.size()<1)
{
node *child0=new node(q.top());
q.pop();
node *child1=new node(q.top());
q.pop();
q.push(node(child0,child1));
}
cout<<" char Symbol code "<<endl;
q.top().traverse();
return 0;
}
But it shows me some errors. For example unknown size of priority_queue and so on. Here is also the error list
1>------ Build started: Project: HUffman_coding, Configuration: Debug Win32 ------
1> HUffman_coding.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\queue(212): error C2039: 'value_type' : is not a member of 'std::greater<_Ty>'
1> with
1> [
1> _Ty=node
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\queue(212): error C2146: syntax error : missing ',' before identifier 'value_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\queue(212): error C2065: 'value_type' : undeclared identifier
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(100): error C3203: 'less' : unspecialized class template can't be used as a template argument for template parameter '_Pr', expected a real type
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(100): error C2955: 'std::less' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(121) : see declaration of 'std::less'
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(100): error C2133: 'q' : unknown size
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(100): error C2512: 'std::priority_queue' : no appropriate default constructor available
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(103): error C2663: 'std::priority_queue<_Ty,_Container,_Pr>::push' : 2 overloads have no legal conversion for 'this' pointer
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(104): error C2662: 'std::priority_queue<_Ty,_Container,_Pr>::size' : cannot convert 'this' pointer from 'std::priority_queue' to 'const std::priority_queue<_Ty,_Container,_Pr> &'
1> Reason: cannot convert from 'std::priority_queue' to 'const std::priority_queue<_Ty,_Container,_Pr>'
1> Conversion requires a second user-defined-conversion operator or constructor
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(104): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have also recognized that, when I am trying to do like this q.top().traverse() after top. it does not show me traverse option. What is wrong?
I think that instead of
priority_queue<vector<node>,greater<node> >q;
you meant to write:
priority_queue<node, vector<node>, greater<node> >q;
That should solve the errors you're seeing at the moment.
And like I suggested, please do properly format your code. It will make everything so much easier to read and probably makes receiving answers more likely.
i have following code
uint32 joaat_hash(uchar *key, size_t len)
{
uint32 hash = 0;
size_t i;
for (i = 0; i < len; i++)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
what will be it's equivalent code in c++? i means data types
1>------ Build started: Project: hash_functions, Configuration: Debug Win32 ------
1> hash_function.cpp
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ';' before identifier 'joaat_hash'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'uchar' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'key' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1> c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ')' before identifier 'len'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2078: too many initializers
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1> c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Include <cstdint> (compiler must support C++0x) and change following datatypes:
uint32 -> uint32_t (or std::uint32_t)
uchar -> unsigned char
As mentioned in comment, cstdint won't be available yet in all compilers, most of the time you can use stdint.h however (the normal C99 header, not in std namespace).
Add these to the top of your code:
typedef unsigned char uchar;
typedef unsigned long uint32; // likely, but not guarenteed.
//typedef unsigned int size_t; // should be defined in stdlib.h
If by "equivalent code in C++" you mean if this should be refactored to use language constructs only available in C++ such as templates or classes, I would say it doesn't.
There are no obvious invariants in the function so it doesn't really form the basis of an object.
The nature of the data to be processed doesn't scale to arbitrary types so templates don't seem a good fit either.
I think the comment suggesting the use of stdint.h is very sensible advice and I would also replace those magic numbers with something more implicitly meaningful (int const's, etc.).
That, to me, seems to already be a C++ code. I wonder why you think it's not.
But since C++ doesn't have uint and uchar explicitly defined you'll need to either have this piece of code before that function.
typedef unsigned int uint32;
typedef unsigned char uchar;
Otherwise, rewrite the function like:
unsigned int joaat_hash(unsigned char *key, size_t len)
{
unsigned int hash = 0;
size_t i;
for (i = 0; i < len; i++)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}