Class and construct Error C++ - c++

I need help with this error?
molecule.cpp:31:7: error: qualified reference to 'mole' is a constructor name rather than a type wherever a constructor can be declared
mole::mole(Atom1(), Atom2() ){
class mole {
private:
string name;
int proton;
int neutron;
int electron;
int valence;
public:
int mass();
mole();
mole(Atom, Atom);
mole(string);
mole(string,int,int,int);
};
mole::mole()
{
name="hydrogen";
proton=1;
neutron=0;
electron=1;
valence=1;
}
mole::mole(Atom1(), Atom2() ){
proton= Atom1.p + Atom2.p;
neutron=Atom1.n + Atom2.n;
electron=Atom1.e + Atom2.e;
}
In another file:
#include<iostream>
using namespace std;
class Atom {
private:
string name;
int proton;
int neutron;
int electron;
int valence;
public:
int mass();
Atom();
Atom(int,int,int);
Atom(string);
Atom(string,int,int,int);
};
Atom::Atom(){
name="hydrogen";
proton=1;
neutron=0;
electron=1;
valence=1;
}
Atom::Atom(int p, int n, int e){
proton=p;
neutron=n;
electron=e;
}
Atom::Atom(string n){
name=n;
}
Atom::Atom(string nm, int p, int n, int e){
name = nm;
proton=p;
neutron=n;
electron=e;
}
int Atom::mass(){
int mass = proton+neutron;
return mass;
}

I'm assuming that Atom is a class that is declared elsewhere? If you wanted to be able to accept two parameters of type Atom in the non-default constructor, you should declare it like so:
mole(Atom atom1, Atom atom2);
...
mole::mole(Atom atom1, Atom atom2) {
proton = atom1.p + atom2.p
....
}

So many errors. Let's tidy it up a bit and make it real C++.
mole::mole( const Atom & a, const Atom & b )
: proton( a.p + b.p )
, neutron( a.n + b.n )
, electron( a.e + b.e )
, valence{} // or whatever calculation you intended
{
}

Related

default value for a pointer parameter

I'm trying to create a class for employees, and have a problem with its constructor.
My class looks like that (please note the name parameter which is char* type):
class Employee {
int id;
char * name ;
float salary;
int hours;
int extra;
public:
//constructor
Employee(int, char *, float, int, int);
//Getters and Setters:
void setId(int a) { id = a; }
int getId() { return id; }
void setName(char * c) { name = c; }
char * getName() { return name; }
void setSalary(float f) { salary = f; }
float getSalary() { return salary; }
void setHours(int h) { hours = h; }
int getHours() { return hours; }
void setExtra(int e) { extra = e; }
int getExtra() { return extra; }
};
I built a constructor and I want it to have default parameters, and I don't know how to deal with the name parameter to have a default of let's say "unknown".
This is the constructor:
Employee::Employee(int i = 123456789, char * na, float sal = 30, int ho = 45, int ex = 10)
{
id = i;
name = na;
salary = sal;
hours = ho;
extra = ex;
}
You can use a character array, and initialise the array to point to the first element of the array. You can store the array for example as a static member:
// in the class definition
inline static char default_name[] = "unknown";
// in argument list of the constructor
... char * na = default_name, ...
Although, you may want to consider whether it makes sense for name to be pointer to non-const. Perhaps a pointer to const would suffice. In such case, you could initialise it to point to the literal "unknown" directly.
A cleaner version
class Employee {
int id = 0;
char *name = nullptr;
float salary = 0.0;
int hours = 0;
int extra = 0;
And you don't need to have constructors, this depends on the case, but you get the idea that by initializing the variables on the definition you reduce the inconsistency of having multiples constructors for example

How can I access a member of a class from a function via pointer?

I wish to
create an array of class/struct items (c1)
then create an array of pointer to the original array (*cp1), which can be sorted
then access members of the class from within a function.
However I'm getting stuck at the initial function call.
Here's my basic code:
struct Car
{
int speed;
};
Car c1[5];
Car *cp1[5];
int main() {
for (int i=0;i<5;i++) {
c1[i].speed = i;
cp1[i] = &c1[i];
}
garage(cp1, 5);
}
void garage(Car **ar, int n) {
int p = (*ar[n / 2])->speed;
}
First of all, your garage function is not known to the compiler at the place where you call it, since it is defined below main. To fix it, either place the function definition above main, or introduce it with a prototype.
Second, at the line int p = (*ar[n / 2])->speed;, *ar[n/2] is not a pointer, so you should use . instead of ->, as in int p = (*ar[n / 2]).speed;
Funcion garage must be declared before you can refer it.
void garage(Car **ar, int n);
int main()
{
//...
}
void garage(Car **ar, int n) {
//...
}
Function main in C++ shall have return type int
int main()
{
//...
}
And within the function the correct expression will look
void garage(Car **ar, int n) {
int p = (*ar )[n / 2]).speed;
}
Or
void garage(Car **ar, int n) {
int p = ar[n / 2]->speed;
}
Or
void garage(Car **ar, int n) {
int p = ( *ar[n / 2] ).speed;
}
struct Car
{
int speed;
};
Car c1[5];
Car *cp1[5];
void garage(Car **ar, int n); // forward declare garage
int main()
{
for (int i=0;i<5;i++) {
c1[i].speed = i;
cp1[i] = &c1[i];
}
garage(cp1, 5);
}
void garage(Car **ar, int n) {
int p = ar[n / 2]->speed; // -> dereferences the pointer, you don't need to
}

Is it possible to take two values of one class to another class and use it?

I am a beginner. And, i tried this one.
#include <iostream>
using namespace std;
class Square
{
int Number;
public:
Square(int a): Number(a) {}
int getSquare()
{
return Number*Number;
}
};
class SumNumber
{
Square a;
int FirNum;
int SecNum;
public:
SumNumber(int Number, int x, int y): a(Number),FirNum(x), SecNum(y) {}
int getSumNumber() //output-er
{
return FirNum + SecNum + a.getSquare();
}
};
int main() {
SumNumber a(2,3,4); //sums up squared number 2, and 3 and 4
cout << "Sum of Numbers\t" << a.getSumNumber() << endl; //=11
}
But, what if I wanted to have 2 values of class Square
Class Square
{
int Number;
int NextNumber;
public:
...
};
and on the other class
class SumNumber //creating class SumNumber
{
Square a;
int FirNum; //first number
int SecNum; //second number
public:
...
};
Question is:
Is it possible to take two values declared in Square class into SumNumber? If so, how?
I think what you are looking for is having a getter. it is just a method that can return a the value of a private member, since your square class is returning the square of a number
you can change it as follows:
class Square
{
int Number;
int nextNumber;
public:
Square(int a, int b): Number(a), nextNumber(b) {}
int getNumberSquared()
{
return Number*Number;
}
int getNextNumberSquared()
{
return nextNumber*nextNumber;
}
};
and in your other class you can have this:
class SumNumber
{
Square a;
int FirNum;
int SecNum;
public:
SumNumber(int Number, int nextNumber,int x, int y):
a(Number, nextNumber),FirNum(x), SecNum(y) {}
int getSumNumber() //output-er
{
return FirNum + SecNum + a.getNumberSquared() + a.getNextNumberSquared();
}
};

Use push_back() to add an item in a vector, the size of the vector increase but can't read values from the vector

I have a class ColorName, and a class ColorNameLookup. In class ColorNameLookup, there is a public member: vector colorList. And the constructor of class ColorNameLookup is to add items into the vector colorList.
I add 140 items into the vector colorList. I checked the size of the vector is correct, but I can't read any value from the vector.
For instance, I initialize a variable ColorNameLookup findColor, and use function findColor.colorList[0].getR() to get the first element's R value. The return is an uninitialized integer value.
my code is shown below:
Class ColorName:
#include "ColorName.h"
ColorName::ColorName(std::string name, int r, int g, int b)
{
r = r;
g = g;
b = b;
name = name;
}
ColorName::ColorName(void)
{
}
ColorName::~ColorName(void)
{
}
int ColorName::computeMSE(int pixR, int pixG, int pixB) {
return ((pixR-r)*(pixR-r) + (pixG-g)*(pixG-g) + (pixB-b)*(pixB-b)/3);
}
int ColorName::getR() {
return r;
}
int ColorName::getG() {
return g;
}
int ColorName::getB() {
return b;
}
std::string ColorName::getName() {
return name;
}
Class ColorNameLookup:
ColorNameLookup::ColorNameLookup(void)
{
colorList.push_back(ColorName("AliceBlue",0xF0,0xF8,0xFF));
//...139 push_back
colorList.push_back(ColorName("YellowGreen",0x9A,0xCD,0x32));
}
ColorNameLookup::~ColorNameLookup(void)
{
}
std::string ColorNameLookup::getColorName(int r, int g, int b) {
ColorName closestMatch; // = NULL;
int findFlag = 0;
int minMSE = std::numeric_limits<int>::max();
int mse;
for (ColorName c : colorList) {
mse = c.computeMSE(r, g, b);
if (mse < minMSE) {
findFlag = 1;
minMSE = mse;
closestMatch = c;
//printf("Find color!\n");
}
}
if (findFlag ==1){
return closestMatch.getName();
}
else
return NULL;
}
You'd need this:
ColorName::ColorName(std::string name, int r, int g, int b)
{
this->r = r;
this->g = g;
this->b = b;
this->name = name;
}
r = r would just assign the same local r to itself, and leave the member variables uninitialized.
For initializing these members, you'd be better off with an initializer list though:
ColorName::ColorName(std::string name, int r, int g, int b) :
r(r),
g(g),
b(b),
name(name)
{
}

matrix in a class in C++

the following code is for three classes , class One, class Two, class Three.
class Three takes tow vectors, the first vector contains instances of One , the second vector contains instances of Two.
I want to get a 2D matrix via a method in Three , this matrix will have two equal indices each one is the size of the vector of One instances.
I don't know where to declare this matrix , and how to initialize it.
i will present a code is working fine before i declare the matrix , then i will present one example of my many tries which is not working and producing error messages.
here is the code before declaring the matrix(it works fine)
#include<iostream>
#include<vector>
#include <stdlib.h>
using namespace std;
const unsigned int N = 5;
class One
{
private:
unsigned int id;
public:
unsigned int get_id(){return id;};
void set_id(unsigned int value) {id = value;};
One(unsigned int init_val = 0): id(init_val) {}; // constructor
~One() {}; // destructor
};
class Two
{
private:
One first_one;
One second_one;
unsigned int rank;
public:
unsigned int get_rank() {return rank;};
void set_rank(unsigned int value) {rank = value;};
unsigned int get_One_1(){return first_one.get_id();};
unsigned int get_One_2(){return second_one.get_id();};
Two(const One& One_1 = 0, const One& One_2 =0 , unsigned int init_rank = 0)
: first_one(One_1), second_one(One_2), rank(init_rank)
{
}
~Two() {} ; // destructor
};
class Three
{
private:
std::vector<One> ones;
std::vector<Two> twos;
public:
Three(vector<One>& one_vector, vector<Two>& two_vector)
: ones(one_vector), twos(two_vector)
{
}
~Three() {};
vector<One> get_ones(){return ones;};
vector<Two> get_twos(){return twos;};
void set_ones(vector<One> vector_1_value) {ones = vector_1_value;};
void set_twos(vector<Two> vector_2_value) {twos = vector_2_value;};
};
int main()
{
cout<< "Hello, This is a draft for classes"<< endl;
vector<One> elements(5);
cout<<elements[1].get_id()<<endl;
vector<Two> members(10);
cout<<members[8].get_One_1()<<endl;
Three item(elements, members);
cout<<item.get_ones()[3].get_id() << endl;
return 0;
}
now i declared a method to produce a matrix in Three , the method's name is get_Mat() here is the code :
#include<iostream>
#include<vector>
#include <stdlib.h>
using namespace std;
const unsigned int N = 5;
class One
{
private:
unsigned int id;
public:
unsigned int get_id(){return id;};
void set_id(unsigned int value) {id = value;};
One(unsigned int init_val = 0): id(init_val) {}; // constructor
~One() {}; // destructor
};
class Two
{
private:
One first_one;
One second_one;
unsigned int rank;
public:
unsigned int get_rank() {return rank;};
void set_rank(unsigned int value) {rank = value;};
unsigned int get_One_1(){return first_one.get_id();};
unsigned int get_One_2(){return second_one.get_id();};
Two(const One& One_1 = 0, const One& One_2 =0 , unsigned int init_rank = 0)
: first_one(One_1), second_one(One_2), rank(init_rank)
{
}
~Two() {} ; // destructor
};
class Three
{
private:
std::vector<One> ones;
std::vector<Two> twos;
public:
Three(vector<One>& one_vector, vector<Two>& two_vector)
: ones(one_vector), twos(two_vector)
{
}
~Three() {};
vector<One> get_ones(){return ones;};
vector<Two> get_twos(){return twos;};
void set_ones(vector<One> vector_1_value) {ones = vector_1_value;};
void set_twos(vector<Two> vector_2_value) {twos = vector_2_value;};
unsigned int get_Mat() {
unsigned int mat[ones.size()][ones.size()];
for(unsigned int i = 0; i < ones.size(); ++i)
for(unsigned int j = 0; j < ones.size(); ++j)
mat[i][j] = 1;
return mat;}
};
int main()
{
cout<< "Hello, This is a draft for classes"<< endl;
vector<One> elements(5);
cout<<elements[1].get_id()<<endl;
vector<Two> members(10);
cout<<members[8].get_One_1()<<endl;
Three item(elements, members);
cout<<item.get_ones()[3].get_id() << endl;
return 0;
}
I will be very thankful if you can help me to find a way to produce this matrix via a method in class Three.
Thanks.
get_Mat returns an integer, not a matrix. It is better to use vector<vector<unsigned int> >, that will avoid a lot of troubles later on.
Or have a look here (c++):
Return a 2d array from a function
or here (C):
Return a 2d array from a function