I am getting an error extra qualification ‘student::’ on member ‘student’ [-fpermissive].
And also why name::name such syntax is used in constructor?
#include<iostream>
#include<string.h>
using namespace std;
class student
{
private:
int id;
char name[30];
public:
/* void read()
{
cout<<"enter id"<<endl;
cin>>id;
cout<<"enter name"<<endl;
cin>>name;
}*/
void show()
{
cout<<id<<name<<endl;
}
student::student()
{
id=0;
strcpy(name,"undefine");
}
};
main()
{
student s1;
// s1.read();
cout<<"showing data of s1"<<endl;
s1.show();
// s2.read();
//cout<<"showing data of s2"<<endl;
//s2.show();
}
In-class definitions of member function(s)/constructor(s)/destructor don't require qualification such as student::.
So this code,
student::student()
{
id=0;
strcpy(name,"undefine");
}
should be this:
student()
{
id=0;
strcpy(name,"undefine");
}
The qualification student:: is required only if you define the member functions outside the class, usually in .cpp file.
It would be correct if definition of constructor would appear outside of class definition.
Similar code:
Change from:
class Solution {
public:
static int Solution::curr_h; // ----------------- ISSUE: "Solution::" is extra
static int Solution::curr_m; // ----------------- ISSUE: "Solution::" is extra
};
int Solution::curr_h = 0;
int Solution::curr_m = 0;
to:
class Solution {
public:
static int curr_h; // ----------------- FIX: remove "Solution::"
static int curr_m; // ----------------- FIX: remove "Solution::"
};
int Solution::curr_h = 0; // <------ good here - "Solution::" is required
int Solution::curr_m = 0; // <------ good here - "Solution::" is required
This happens because copy pasting statics require initialization outside of class, but also requires all the declaration (int) too.
Though it is correct, wish there is a easier/ better way to do the same.
Related
I am trying to use cin to read private members of a class within a member function of the class and i am getting an error [Error] no match for 'operator>>' in 'std::cin >> ((const Course*)this)->Course::courseID' ?
Is it possible to do that? Here is my code
//Course.h
#ifndef Course_H
#define Course_H
#include<string>
#include<iostream>
using namespace std;
class Course
{
private:
string courseID;
string courseName;
int credits;
Course * nextCourse;
public:
void EnterCourse() const;
void SetNext(Course *);
string GetCourseID() const;
string GetCourseName() const;
int GetCredits() const;
};
void Course::EnterCourse() const
{
cout<<"=======Enter Course Information========\n"
<<"Enter Course ID: \n";
cin>>courseID;
cout<<"Enter Course Name: \n";
cin>>courseName;
cout<<"Enter credits: \n";
cin>>credits;
}
string Course::GetCourseID() const
{
return courseID;
}
string Course::GetCourseName() const
{
return courseName;
}
int Course::GetCredits() const
{
return credits;
}
void Course::SetNext(Course * cou)
{
nextCourse=cou;
}
#endif
Using std::cin is considered writing to member data. But you can not change the data members using a const qualified member function. Remove the const specifier from the function declaration and definition if you want to achieve that functionality:
void Course::EnterCourse();
More info on the subject in these SO posts:
Meaning of “const” last in a C++ method declaration?
What is meant with “const” at end of function declaration?
I am struggling knowing how to create a class. I want to create a "Player" class and all I want to do is pass in the name while I'll have the other variables start at 0 until they are updated when a game is run (later in the program)
Player::Player(string name_in)
{
name = name_in;
int numOfWins = 0;
int numOfLoses = 0;
int numOfDraws = 0;
int totalMatches = 0;
}
Right now there are lots of errors around numOfWins, numOfLoses, numOfDraws and totalMatches. What can I do to fix this?
Perhaps the error is in your int ... part of assignments, which essentially creates a new local variable in a constructor.
Try this version:
#include <string>
using namespace std;
class Player
{
string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
public:
Player(string name_in)
{
name = name_in;
numOfWins = 0;
numOfLoses = 0;
numOfDraws = 0;
totalMatches = 0;
}
};
You should declare other instance variables in the class declaration, rather than declaring them as locals (which is completely useless).
// This part goes in the header
class Player {
string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
public:
Player(string name_in);
};
Now in the constructor you could use initialization lists:
// This part goes into the CPP file
Player::Player(string name_in)
// Initialization list precedes the body of the constructor
: name(name_in), numOfWins(0), numOfLoses(0), numOfDraws(0), totalMatches(0) {
// In this case, the body of the constructor is empty;
// there are no local variable declarations here.
}
Kinda vague, but I'll take a crack at it. You Probably want:
class Player{
string name;
int numOfWins;
int numOfLosses;
int numOfDraws;
int totalMatches;
Player(string name_in)
};
Player::Player(string name_in){
name = name_in;
numOfWins = 0;
numOfLosses = 0;
numOfDraws = 0;
totalMatches = 0;
}
Haven't used C++ in a while, so this may be faulty.
The errors you get, at least from the snippet you posted are caused for you can't declare variables in constructor - you declare them in class body and initialize in constructor or using another function.
#include <string>
class Player {
public:
Player( std::string const& name_in) : name( name_in),
numOfWins(), numOfLoses(),
numOfDraws(), totalMatches()
{} // constructor
// will initialize variables
// numOfWins() means default
// initialization of an integer
private:
std::string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
};
usage:
int main() {
Player( "player_one");
return 0;
}
I have written a class as shown below:
#include<iostream>
using namespace std;
class A
{
static int cnt;
static void inc()
{
cnt++;
}
int a;
public:
A(){ inc(); }
};
int main()
{
A d;
return 0;
}
I want to call the function inc through the constructor, but when i compile i am getting an error as:
/tmp/ccWR1moH.o: In function `A::inc()':
s.cpp:(.text._ZN1A3incEv[A::inc()]+0x6): undefined reference to `A::cnt'
s.cpp:(.text._ZN1A3incEv[A::inc()]+0xf): undefined reference to `A::cnt'
I am unable to understand what the error is... plz help...
Static field is not defined - Take a look at Why are classes with static data members getting linker errors?.
#include<iostream>
using namespace std;
class A
{
static int cnt;
static void inc(){
cnt++;
}
int a;
public:
A(){ inc(); }
};
int A::cnt; //<---- HERE
int main()
{
A d;
return 0;
}
Inside the class static int cnt; is only declared, and need to be defined. In C++ you usually declare in your .h .hpp files and then define your static class members in your .c and .cpp files.
In your case, you need to add
int A::cnt=0; // = 0 Would be better, otherwise you're accessing an uninitialized variable.
I'm having trouble declaring and initializing a char array. It always displays random characters. I created a smaller bit of code to show what I'm trying in my larger program:
class test
{
private:
char name[40];
int x;
public:
test();
void display()
{
std::cout<<name<<std::endl;
std::cin>>x;
}
};
test::test()
{
char name [] = "Standard";
}
int main()
{ test *test1 = new test;
test1->display();
}
And sorry if my formatting is bad, I can barely figure out this website let alone how to fix my code :(
If there are no particular reasons to not use std::string, do use std::string.
But if you really need to initialize that character array member, then:
#include <assert.h>
#include <iostream>
#include <string.h>
using namespace std;
class test
{
private:
char name[40];
int x;
public:
test();
void display() const
{
std::cout<<name<<std::endl;
}
};
test::test()
{
static char const nameData[] = "Standard";
assert( strlen( nameData ) < sizeof( name ) );
strcpy( name, nameData );
}
int main()
{
test().display();
}
Your constructor is not setting the member variable name, it's declaring a local variable. Once the local variable goes out of scope at the end of the constructor, it disappears. Meanwhile the member variable still isn't initialized and is filled with random garbage.
If you're going to use old-fashioned character arrays you'll also need to use an old-fashioned function like strcpy to copy into the member variable. If all you want to do is set it to an empty string you can initialize it with name[0] = 0.
Since you are using C++, I suggest using strings instead of char arrays. Otherwise you'd need to employ strcpy (or friends).
Also, you forgot to delete the test1 instance.
#include <iostream>
#include <string>
class test
{
private:
std::string name;
int x;
public:
test();
void display()
{
std::cout<<name<<std::endl;
}
};
test::test()
{
name = "Standard";
}
int main()
{
test test1;
test1.display();
std::cin>>x;
}
Considering you tagged the question as C++, you should use std::string:
#include <string>
class test
{
private:
std::string name;
int x;
public:
test();
void display()
{
std::cout<<name<<std::endl;
std::cin>>x;
}
};
test::test() : name("Standard")
{
}
c++11 actually provides two ways of doing this. You can default the member on it's declaration line or you can use the constructor initialization list.
Example of declaration line initialization:
class test1 {
char name[40] = "Standard";
public:
void display() { cout << name << endl; }
};
Example of constructor initialization:
class test2 {
char name[40];
public:
test2() : name("Standard") {};
void display() { cout << name << endl; }
};
You can see a live example of both of these here: http://ideone.com/zC8We9
My personal preference is to use the declaration line initialization because:
Where no other variables must be constructed this allows the generated default constructor to be used
Where multiple constructors are required this allows the variable to be initialized in only one place rather than in all the constructor initialization lists
Having said all this, using a char[] may be considered damaging as the generated default assignment operator, and copy/move constructors won't work. This can be solved by:
Making the member const
Using a char* (this won't work if the member will hold anything but a literal string)
In the general case std::string should be preferred
I have the following structure:
template <class T>
struct Array{
int lenght;
T * M;
Array( int size ) : lenght(size), M(new T[size])
{
}
~Array()
{
delete[] M;
}
};
And the class(of objects which will fill the structure):
class Student{
private:
int ID;
int group;
char name[];
public:
Student();
~Student();
void setStudent(int,int,char){
}
char getName(){
return *name;
}
void getGroup(){
}
void getID(){
}
};
Now when I want to initialize the Array type, I get the following in Main.cpp:
#include <iostream>
#include "Domain.h"
#include "Student.h"
//#include ""
using namespace std;
int main(){
cout<<"start:"<<endl<<endl;
Array <Student> DB(50);
Array <Student> BU(50);
return 0;
}
ERROR:
g++ -o Lab6-8.exe UI.o Repository.o Main.o Domain.o Controller.o
Main.o: In function `Array':
D:\c++\Begin\Lab6-8\Debug/..//Domain.h:16: undefined reference to `Student::Student()'
D:\c++\Begin\Lab6-8\Debug/..//Domain.h:16: undefined reference to `Student::~Student()'
Main.o: In function `~Array':
D:\c++\Begin\Lab6-8\Debug/..//Domain.h:21: undefined reference to `Student::~Student()'
Any idea why?
When you write:
class Student
{
public:
Student();
~Student();
};
You have explicitly declared class constructor and destructor so compiler didn't define them for you - you need to provide their definition (implementation). In trivial case, this would do the job:
class Student
{
public:
Student(){};
~Student(){};
};
This is because you have declared a constructor and a destructor of Student, but you are missing their definitions.
You can provide these definitions inline as part of the declaration of Student, presumably in the .h file:
Student() {
// initialize the student
}
~Student() {
// release dynamically allocated parts of the student
}
or outside your class declaration in a cpp file:
Student::Student() {
// initialize the student
}
Student::~Student() {
// release dynamically allocated parts of the student
}
As a side note, name should probably be std::string, not char unless you really want one-letter names.