Weird C++ error, unqualified ID, redeclaration - c++

I want to create a project, I have 3 files, a test.cpp, something.h and a something.cpp. here they are:
test.cpp:
#include <bits/stdc++.h>
#define f cin
#define g cout
#include "something.h"
using namespace std;
int main(void)
{
int x;
register(x);
return 0;
}
something.h:
#ifndef __SOMETHING__H_
#define __SOMETHING__H_
#include <bits/stdc++.h>
void register(int x);
#endif
something.cpp:
#include "something.h"
void register(int x)
{
std::cout << x << '\n';
}
And here is the error I get:
In file included from test.cpp:4:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
test.cpp: In function ‘int main()’:
test.cpp:10:15: error: ISO C++ forbids declaration of ‘x’ with no type [-fpermissive]
register(x);
^
test.cpp:10:15: error: redeclaration of ‘int x’
test.cpp:9:9: note: ‘int x’ previously declared here
int x;
^
In file included from something.cpp:1:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
something.cpp:3:15: error: expected unqualified-id before ‘int’
void register(int x)
^~~
something.cpp:3:15: error: expected ‘)’ before ‘int’
Why does it tell me that I redefine x? When I just want to call register with it's value.

register is a reserved word in C++. Therefore, you have to give another (unreserved) name.
more information: Register keyword in C++ - Stack Overflow

Related

Fail to compile: strlen is not a member of std

I'm trying to compile this very simple program where I'm implementing a simplified version of C++ strings.
However the compiler cannot find the std::strlen function even though I included
//main.cpp
#include "str.h"
int main()
{
return 0;
}
// str.h
#ifndef _STRING_H_
#define _STRING_H_
#include <vector>
#include <cstring>
#include <algorithm>
#include <iterator>
class Str {
public:
typedef std::vector<char>::size_type size_type;
Str() { }
Str(size_type n, char c): data(n, c) { }
Str(const char* cp) {
std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
}
template <class In> Str(In b, In e) {
std::copy(b, e, std::back_inserter(data));
}
private:
std::vector<char> data;
};
#endif
I'm compiling using g++ (homebrew)
g++ main.cpp -o main
and here's the result log
In file included from str.h:5,
from main.cpp:1:
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:75:11: error: '::memchr' has not been declared
using ::memchr;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:76:11: error: '::memcmp' has not been declared
using ::memcmp;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:77:11: error: '::memcpy' has not been declared
using ::memcpy;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:78:11: error: '::memmove' has not been declared
using ::memmove;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:79:11: error: '::memset' has not been declared
using ::memset;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:80:11: error: '::strcat' has not been declared
using ::strcat;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:81:11: error: '::strcmp' has not been declared
using ::strcmp;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:82:11: error: '::strcoll' has not been declared
using ::strcoll;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:83:11: error: '::strcpy' has not been declared
using ::strcpy;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:84:11: error: '::strcspn' has not been declared
using ::strcspn;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:85:11: error: '::strerror' has not been declared
using ::strerror;
^~~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:86:11: error: '::strlen' has not been declared
using ::strlen;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:87:11: error: '::strncat' has not been declared
using ::strncat;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:88:11: error: '::strncmp' has not been declared
using ::strncmp;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:89:11: error: '::strncpy' has not been declared
using ::strncpy;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:90:11: error: '::strspn' has not been declared
using ::strspn;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:91:11: error: '::strtok' has not been declared
using ::strtok;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:92:11: error: '::strxfrm' has not been declared
using ::strxfrm;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:93:11: error: '::strchr' has not been declared
using ::strchr;
^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:94:11: error: '::strpbrk' has not been declared
using ::strpbrk;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:95:11: error: '::strrchr' has not been declared
using ::strrchr;
^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:96:11: error: '::strstr' has not been declared
using ::strstr;
^~~~~~
In file included from main.cpp:1:
str.h: In constructor 'Str::Str(const char*)':
str.h:19:31: error: 'strlen' is not a member of 'std'
std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
^~~~~~
str.h:19:31: note: suggested alternative: 'strstr'
std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
^~~~~~
strstr
Am I doing something wrong here ? I don't think I missed any headers or namespace identifier so it should compile.
The macro name _STRING_H_ is reserved for the C standard to use. Defining such macro is undefined behavior.
You'r include guard around str.h is _STRING_H_ and you define it. But, the same include guard is most probably used in string.h standard C header. So all symbols from string.h will be not visible in your program. As you could get lucky with glibc, as it uses _STRING_H, but I could find many implementations that use just the string _STRING_H_ as include guards around their files.
To fix the issue, change your include guard from _STRING_H_ to example STR_H_ or STRING_H_ without the leading underscore. Remember, about reserved names by the C standard when writing programs.

How to fix expected primary-expression?

#include<iostream>
#include<memory>
class test{
public:
void print()
{
std::cout<<"test print"<<std::endl;
}
};
int main
{
std::auto_ptr<test> t1 (new test);
t1->print();
return 0;
}
I am getting following error:
$g++ 5.cpp --std=c++11
5.cpp:16:22: error: expected primary-expression before ‘t1’
std::auto_ptr<test> t1 (new test);
^
5.cpp:16:22: error: expected ‘}’ before ‘t1’
5.cpp:16:22: error: expected ‘,’ or ‘;’ before ‘t1’
5.cpp:17:2: error: ‘t1’ does not name a type
t1->print();
^
5.cpp:19:2: error: expected unqualified-id before ‘return’
return 0;
^
5.cpp:20:1: error: expected declaration before ‘}’ token
}
^
int main // <-- Notice anything ?
the problem is, that you forgot the parantheses in main.
int main { ...} // this is wrong!
but the right would be
int main() { ... }

How to fix and compile this C++ program

In nestClassDef.h I have written code like this
class A{
public:
class B{
public:
void BTest();
};
};
class B{
};
then in the nestClassDef.cpp I am writing code like this
#include "nestClassDef.h"
#include<iostream>
void A::B::BTest(){
cout<<"Hello World!";
}
int main(){
A a;
A.B b;
b.BTest();
}
But when I am compiling the above code
g++ -o nestClassDef nestClassDef.cpp
I am getting error like this :-
nestClassDef.cpp: In member function ‘void A::B::BTest()’:
nestClassDef.cpp:5: error: ‘cout’ was not declared in this scope
nestClassDef.cpp: In function ‘int main()’:
nestClassDef.cpp:10: error: expected unqualified-id before ‘.’ token
nestClassDef.cpp:11: error: ‘b’ was not declared in this scope
I am at a loss how to fix this. Any understanding shared will be thankfully received.
For the cout error: it's in std namespace, so use std::cout.
For The second error: B is not A's member, it's a nested type, so you have to use A::B b;
nestClassDef.cpp: In member function ‘void A::B::BTest()’:
nestClassDef.cpp:5: error: ‘cout’ was not declared in this scope
Use std::cout instead of cout, or add using namespace std; (probably after your #include statements).
nestClassDef.cpp: In function ‘int main()’:
nestClassDef.cpp:10: error: expected unqualified-id before ‘.’ token
nestClassDef.cpp:11: error: ‘b’ was not declared in this scope
Use A::B instead of A.B.
Add using namespace std; or use std::cout instead of just cout
Use A::B not A.B. Dot operator is used on objects or structs/unions.

reading struct -- uint32 does not name a type

#include <iostream>
#include <map>
#include <zlib.h>
#include <vector>
#include <stdint.h>
using namespace std;
int main (int argc, char **argv)
{
if(argc <2){ exit(0);}
//map<int, int> myMap;
struct last_touch
{
vector<uint64> baz;
uint32 foo;
uint32 bar;
}myLastTouch;
gzFile m_fHandle;
m_fHandle = gzopen(argv[1], "rb");
while(!gzeof(m_fHandle))
{
gzread(m_fHandle,&myLastTouch, sizeof(last_touch));
vector<uint64>::size_type sz = myLastTouch.baz.size();
cout<<"size \t"<<sz<<endl;
}
gzclose(m_fHandle);
}
I'm trying to read a struct from a compressed file.
and I compile it using g++ -lz test.cpp
In function ‘int main(int, char**)’:
test.cpp:15: error: ‘uint64’ was not declared in this scope
test.cpp:15: error: template argument 1 is invalid
test.cpp:15: error: template argument 2 is invalid
test.cpp:16: error: ‘uint32’ does not name a type
test.cpp:17: error: ‘uint32’ does not name a type
test.cpp:27: error: ‘uint64’ cannot appear in a constant-expression
test.cpp:27: error: template argument 1 is invalid
test.cpp:27: error: template argument 2 is invalid
test.cpp:27: error: expected initializer before ‘sz’
These are the following errors I get. I think uint32 is because of <stdint.h>and therefore i included it.
Is there somethign else that i'm missing
Those types should be with _t postfix: uint64_t

Complex number printing program malfunctioning

I have started working on C++ language.I am very new to it.The program takes a complex number from the user and prints it out.But its giving me many errors like,
prog.cpp: In function ‘int main()’:
prog.cpp:26: error: ‘GetReal’ was not declared in this scope
prog.cpp:26: error: ‘SetReal’ was not declared in this scope
prog.cpp:27: error: ‘GetImag’ was not declared in this scope
prog.cpp:27: error: ‘SetImag’ was not declared in this scope
prog.cpp:28: error: ‘print’ was not declared in this scope
prog.cpp: At global scope:
prog.cpp:34: error: expected unqualified-id before ‘)’ token
prog.cpp:40: error: expected unqualified-id before ‘float’
prog.cpp:40: error: expected `)' before ‘float’
prog.cpp: In function ‘void SetImag(float)’:
prog.cpp:64: error: ‘real’ was not declared in this scope
prog.cpp: In function ‘void SetReal(float)’:
prog.cpp:68: error: ‘imag’ was not declared in this scope
prog.cpp: In function ‘void print()’:
prog.cpp:73: error: ‘Cout’ was not declared in this scope
prog.cpp:73: error: ‘real’ was not declared in this scope
prog.cpp:73: error: ‘imag’ was not declared in this scope
Here's the code:
/*
Date=13 January 2011
Program: To take a complex number from the user and print it on the screen */
/*Defining a class*/
#include <iostream>
using namespace std;
class complex
{
float real;
float imag;
public:
complex();
complex(float a,float b);
float GetReal();
float GetImag();
void SetReal();
void SetImag();
void print();
};
int main()
{
complex comp;
SetReal(GetReal());
SetImag(GetImag());
print();
}
complex()
{
real=0;
imag=0;
}
complex(float a,float b)
{
real=a;
imag=b;
}
float GetReal()
{
float realdata;
cout<<"Enter Real part:"<<endl;
cin>>realdata;
return realdata;
}
float GetImag()
{
float imagdata;
cout<<"Enter Imaginary part"<<endl;
cin>>imagdata;
return imagdata;
}
void SetImag(float a)
{
real=a;
}
void SetReal(float b)
{
imag=b;
}
void print()
{
printf("The Complex number is %f+%fi",real,imag);
}
Since GetReal() et al are declared as part of the complex class, you should call them on the object you created:
complex comp;
comp.SetReal(comp.GetReal());
comp.SetImag(comp.GetImag());
comp.print();
Similarly, you need to scope the implementation of the complex constructor:
complex::complex()
{
real=0;
imag=0;
}
The same applies to the other member functions not shown in your post.
In your main function you need to call GetReal and SetReal on an instance of the class:
e.g.
Complex comp;
comp.SetReal();
...
Also, your method bodies aren't bound to the class, they're floating in the global namespace. You need to define them:
void Complex::SetReal() {} //etc
Hope this helps