How to initialize string in Class in c++ (g++ ) - c++

I'm getting this error while initializing a string in a class
Error :
publicclass.cpp:13:6: error: array type 'char [50]' is not assignable
s.n = "Randomstring";
But char is working. Only getting error with strings
#include<iostream>
using namespace std;
class student
{
public:
int ht;
char n[50];
};
int main()
{
student s;
s.ht = 1;
s.n = "Randomstring";
cout<<"Hallticket no : "<<s.ht<<"\n";
cout<<"Name : "<<s.n<<"\n";
return 0;
}
Compiler version is g++ 8.0.0 (getting same error with clang++ ,turboc++(in windows))

Arrays are not assignable.
You can initialise the member when you initialise the object that contains it:
student s{1, "Randomstring"};
You can copy the elements of an existing array after initialisation:
std::strncpy(s.n, "Randomstring", std::size(s.n));

Related

Random number example from Stroustrup has compilation error

I'm using this random number example from Stroustrup C++ 4th Ed Page 1182. The compiler is reporting an error on the line with auto, stating that auto cannot be used with non-static members of classes. I'm confused on what the type of this bind results in. Does anyone know how to resolve the error so the random number generator can be used?
#include <random>
using namespace std;
class Rand_int {
public: // added
Rand_int(int lo, int hi) : p{lo,hi} { }
int operator()() const { return r(); }
private:
uniform_int_distribution<>::param_type p;
auto r = bind(uniform_int_distribution<>{p},default_random_engine{});
};
int main()
{
Rand_int ri {0,10};
int pz = ri();
return 0;
}
Compilation error:
clang++ -Wall -std=c++11 -pedantic test252.cc && ./a.out
test252.cc:11:5: error: 'auto' not allowed in non-static class member
auto r = bind(uniform_int_distribution<>{p},default_random_e...
^~~~
You can't use auto for the type of a non-static member of a class, so the code example is wrong.
Instead, you can do:
class Rand_int {
private:
std::function<int()> r = bind(uniform_int_distribution<>{p},default_random_engine{});
// ...
};
This converts the return type of std::bind to a void function returning an int, which is the desired behavior.
Here's a demo.

X does not name a type in c++ [duplicate]

This question already has answers here:
"Y does not name a type" error in C++
(2 answers)
Closed 2 years ago.
I am learning c++, in particular, I am learning about inheritance. I wrote the following code where I wanted to print the contents of protected_stuff, a variable defined within the protected access specifier in the MainClass.
Here is my code:
inheritance.cpp
#include<iostream>
#include"MainClass.h"
#include"DerivedClass.h"
int main(){
DerivedClass a;
a.func();
return 0;
}
DerivedClass.h
#ifndef DERIVEDCLASS
#define DERIVEDCLASS
class DerivedClass: public MainClass{
private:
int val;
protected:
int val2;
public:
int val3;
void func(void){
std::cout<<protected_stuff;
}
};
#endif
MainClass.h
#ifndef MAINCLASS
#define MAINCLASS
class MainClass{
private:
int value;
char charecter;
value= 10;
charecter='a';
protected:
int protected_stuff;
protected_stuff = 2;
public:
int public_stuff;
public_stuff = 3;
};
#endif
When I try to run g++ -I . inheritance.cpp I get the following errors:
In file included from inheritance.cpp:2:
MainClass.h:11:2: error: 'value' does not name a type
11 | value= 10;
| ^~~~~
MainClass.h:12:2: error: 'charecter' does not name a type
12 | charecter='a';
| ^~~~~~~~~
MainClass.h:16:2: error: 'protected_stuff' does not name a type
16 | protected_stuff = 2;
| ^~~~~~~~~~~~~~~
MainClass.h:21:2: error: 'public_stuff' does not name a type
21 | public_stuff = 3;
| ^~~~~~~~~~~~
Later I modified MainClass.h and the code works fine.
#ifndef MAINCLASS
#define MAINCLASS
class MainClass{
private:
int value= 10;
char charecter='a';
// value= 10;
// charecter='a';
protected:
int protected_stuff= 2;
// protected_stuff = 2;
public:
int public_stuff = 3;
// public_stuff = 3;
};
#endif
My question is what was I doing wrong? I tried looking at various other questions on SO but couldn't find something similar:
error: 'x' does not name a type
error: ‘X’ does not name a type X in template functions
"X does not name a type" error in C++
"Y does not name a type" error in C++
Error C++ : does not name a type
Another "x" does not name a type error
Correct place to initialize class variables?
Initialisation and assignment
Your errors have nothing to do with inheritance. The same code would be an error without any inheritance.
In C++ statements like value = 10; must be placed inside functions or constructors. So this is OK (statement inside a constructor)
class MainClass{
int value= 10;
MainClass()
{
value = 10;
}
};
so is this (statement inside a function)
class MainClass{
int value= 10;
void function()
{
value = 10;
}
};
On the other hand int value = 10; is a declaration so it can go outside of a function.
This is very basic C++ syntax and semantics. You need to understand the difference between a statement and a declaration. It suggests to me that you need to spend some more time learning the basics of C++ before you tackle inheritance.

cannot convert 'int (B::*)(std::string)' to 'int (*)(std::string) ' in assignment pt2function=&B::generate_callback;

I am new to c++, .I am trying to create a pgm that contains 2 classes ,out of which one class has a member function that would generate a callback function in another class though a function pointer, but i keep getting the following error.
#include <iostream>
#include <string>
using namespace std;
class B
{
private: std::string str1;
public: int generate_callback(std::string str1);
};
int B::generate_callback(std::string str1)
{
if ((str1=="Generate")||(str1=="generate"))
{
Cout<<"Callback generated ";
}
return 0;
}
class A : public B
{
public:
void count(int a,int b);
private: int a,b;
};
void A::count(int a, int b)
{
for ( a=1;a<b;a++){
if(a==50)
{
cout<<"Generating callback ";
goto exit;
}
exit: ;
}
}
int (*pt2function)(string)=NULL;
int main()
{
B obj1;
A obj2;
string str;
cout<<"To generate callback at int i=50 please enter 'generate'";
cin>>str;
obj2.count(1,100);
pt2function=&B::generate_callback;
(obj1.*pt2function)(str);
return 0;
}
The errors :
main.cpp:57: error: cannot convert 'int (B::*)(std::string) {aka int (B::*)(std::basic_string<char>)}' to 'int (*)(std::string) {aka int (*)(std::basic_string<char>)}' in assignment
pt2function=&B::generate_callback;
/home/adt/practice/N_practise/n_pract_2/pract2/main.cpp:58: error: 'pt2function' cannot be used as a member pointer, since it is of type 'int (*)(std::string) {aka int (*)(std::basic_string<char>)}'
(obj1.*pt2function)(str);
^
^
The variable pt2function is a pointer to a non-member function. Such a pointer is not compatible with a pointer to a member-function. Which is what the compiler tells you with the first error: A int (*)(string) is not compatible with a int (B::*)(string).
You need to define pt2function as a pointer to a B member function:
int (B::*pt2function)(string)=NULL;
Now you can initialize or assign a matching member function of B to the variable pt2function.
This also solves the second errors, which basically says that in your current code the variable pt2function is not a pointer to a member function, and therefore can not be used as such.
Pointers to functions and pointers to member functions are really different beasts.
You have mainly two options to get it working in your code:
Change this line:
int (*pt2function)(string)=NULL;
To this:
int (B::*pt2function)(string)=NULL;
That is defining pt2function as a pointer to a member function of B that gets a string and returns an int.
Declare the generate_callback as a static method and invoke it as pt2function(str); in your main function.
In fact, a static member function can be assigned to a pointer to function like the one you have already in use.

Accessing nested class in C++

I came across this question in an online test that I was taking. The task is to alter this program to get rid of compilation errors.
#include<iostream>
#include<iomanip>
class Vehicle
{
public:
static Car* createCar()
{
return new Car;
}
class Car
{
public:
string name;
};
private:
int seats;
};
void useVehicle()
{
Vehicle::Car *c = Vehicle::createCar();
c->name = "BMW";
}
int main(int argc, char *argv[])
{
useVehicle();
return 0;
}
The compilations errors are like:
error: ‘Car’ does not name a type
error: ‘string’ does not name a type
In function void useVehicle():
error: ‘createCar’ is not a member of ‘Vehicle’
How do I get it right ? I tried few things but could not resolve these errors.
error: ‘Car’ does not name a type
At the point of
static Car* createCar()
Car is not yet known. Move the definition of class Car above the function
error: ‘string’ does not name a type In function ‘void useVehicle()’:
#include <string>
also use std:: to qualify string
error: ‘createCar’ is not a member of ‘Vehicle’
This error will disappear once you fix the other two issues. The compiler wasn't able to parse the function declaration because it didn't know what its return type was.

including a std::map within a struct? Is it ok?

class X_class{
public:
struct extra
{int extra1;
int extra2;
int extra3;
};
enum a
{
n,m};
struct x_struct{
char b;
char c;
int d;
int e;
std::map <int, extra> myExtraMap;
};
};
in my code I define :
x_struct myStruct;
why do I get compile errors compiling the above class? The error either says:
1) expected ; before < on the line --- where I defined the map (above) if I eliminate std::
or
2) error: invalid use of ::; error: expected ; before < token
Probably you get erorrs because you didn't #include <map>