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.
Related
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
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.
(Beginner programmer..) I'm following the style of a header file that worked fine, but I'm trying to figure out how I keep getting all of these errors when I compile. I am compiling with g++ in Cygwin.
Ingredient.h:8:13: error: expected unqualified-id before ‘)’ token
Ingredient.h:9:25: error: expected ‘)’ before ‘n’
Ingredient.h:19:15: error: declaration of ‘std::string <anonymous class>::name’
Ingredient.h:12:14: error: conflicts with previous declaration ‘std::string<anonymous class>::name()’
Ingredient.h:20:7: error: declaration of ‘int <anonymous class>::quantity’
Ingredient.h:13:6: error: conflicts with previous declaration ‘int<anonymous class>::quantity()’
Ingredient.h: In member function ‘std::string<anonymous class>::name()’:
Ingredient.h:12:30: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::string’ requested
Ingredient.h: In member function ‘int<anonymous class>::quantity()’:
Ingredient.h:13:25: error: argument of type ‘int (<anonymous class>::)()’ does not match ‘int’
Ingredient.h: At global scope:
Ingredient.h:4:18: error: an anonymous struct cannot have function members
Ingredient.h:21:2: error: abstract declarator ‘<anonymous class>’ used as declaration
And here is my class header file...
#ifndef Ingredient
#define Ingredient
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string name() { return name; }
int quantity() {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif
I am confused by these errors and don't really know what I am doing wrong concerning the implementation of the class..
That's a funny one. You are essentially killing your class name by #define Ingredient - all occurrences of Ingredient will be erased. This is why include guards generally take the form of #define INGREDIENT_H.
You are also using name both for the member and the getter function (probably an attempt to translate C#?). This is not allowed in C++.
How about look on errors? variables and functions can't have same names. And include guard should never names such as class.
#ifndef INGREDIENT_H
#define INGREDIENT_H
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string get_name() const { return name; }
int get_quantity() const {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif
I have written a simple program.
I am getting this error:
time.cpp: In function ‘int main()’:
time.cpp:22:9: error: expected ‘;’ before ‘a’
time.cpp:23:4: error: ‘a’ was not declared in this scope
time.cpp:24:4: error: ‘b’ was not declared in this scope
time.cpp:25:4: error: ‘c’ was not declared in this scope
This is my code:
#include<iostream>
using namespace std;
class time
{
int hour;
int min;
public:
void gettime(int h,int m)
{
hour=h;
min=m;
}
void puttime()
{
cout<<hour<<endl<<min;
}
void sum(time x,time y)
{
min=x.min+y.min;
hour=min/60;
min=min%60;
hour=hour+x.hour+y.hour;
}
};
int main()
{
time a,b,c;
a.gettime(2,45);
b.gettime(3,35);
c.sum(a,b);
a.puttime();
b.putime();
c.puttime();
return 0;
}
Remember that there is a standard function named time.
This is the one main reason you should refrain from using namespace std;.
b.putime() must be b.puttime() here. Otherwise this code compiled
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