class header: error: expected unqualified-id before 'char' while compiling - c++

Sorry for bad english.
I am new on c++ and trying to understand to send references to class object.
But I get above error. If I use const char then error comes up for 'const' part.
Here is my code:
main:
#include <iostream>
#include "DelMe-ClassHeader.h"
using namespace std;
int main() {
char var1 [2];
int var2;
for (int i = 0; i < 2; i++) {
var1[i] = 2;
}
int var2 = 0;
tc = TestClass(var1, var2);
cout << "before tc.changeValue" << endl;
cout << "var1 is " << var1 << endl;
cout << "var2 is " << var2 << endl;
tc.changeValue()
cout << "before tc.changeValue" << endl;
cout << "var1 is " << var1 << endl;
cout << "var2 is " << var2 << endl;
}
header:
#ifndef TestClass
#define TestClass
#include <iostream>
using namespace std;
class TestClass {
public:
TestClass(char (& first)[2]}, int& second);
void changeValue ();
private:
char (& privArray)[2];
int& privInt;
};
#endif
cpp:
#include "DelMe-ClassHeader.h"
#include <iostream>
using namespace std;
TestClass::TestClass(char (& first)[15], int& second) {
this->priveArray = first;
this->privInt = second;
}
void TestClass::changeValue () {
privInt = atoi(privArray);
}
and the error is:
E:\Programing\CodeBlocks\Cpp\DelMe\DelMe\DelMe-ClassHeader.h|10|error: expected unqualified-id before 'char'
I gratefull for any help

The header contains two errors:
#define TestClass defines TestClass as an empty string. Therefore all occurences of TestClass will be replaced with an empty string, hence the errors. For more information read about the C++ preprocessor.
There is an extra } in the parameter list of TestClass.
Replace with this:
#ifndef TestClass_h_inc_
#define TestClass_h_inc_
#include <iostream>
using namespace std;
class TestClass {
public:
TestClass(char(&first)[2], int& second);
void changeValue();
private:
char(&privArray)[2];
int& privInt;
};
#endif

Related

Why copy constructor changed previous object pointer member

#include <iostream>
#include <string.h>
#include "Date.h"
#include "Employee.h"
using std::cout;
using std::endl;
using std::to_string;
class TestOps {
public:
int sex = 1;
string toString() {
return " sex:" + to_string(sex) ;
}
};
class Test {
public:
TestOps* testOps;
Test(const Test& t) :Test{} {
this->testOps = new TestOps{ *(t.testOps) };
};
Test() {
TestOps ops;
//this->testOps = new TestOps{}; // it will be ok with this way
this->testOps = &ops;
}
};
int main() {
// code not understand
Test t1;
cout <<"first testOps:" << t1.testOps->toString() << endl; // sex: 1
Test t2{ t1 };
cout << "first testOps:" << t1.testOps->toString() << endl; // sex: -858893460 ???? why?
cout << "second testOps:" << t2.testOps->toString() << endl; // sex: -858893460 ???? why?
return 0;
}
As you can see, why the first log is as expected while the later logs are not?
Also, t1.testOps address is different from t2.testOps which is as expected.
I have done some research but didn't find the answer. Maybe because I'm pretty new to cpp.

Requesting a function from another file in a class not working

I have 2 .cpp files called "FactoringProgram.cpp" and "FactoringProgram2nd.cpp", also 1 header file called "FactoringProgram.h". I've already tried searching for this problem on StackOverflow and a couple other sites and haven't found a solution that worked for me. When I try to run this command: "g++ FactoringProgram.cpp FactoringProgram2nd.cpp" I get this error:
FactoringProgram.cpp: In function ‘int main()’: FactoringProgram.cpp:8:11: error: request for member ‘InitialMessage’
in ‘Problem1’, which is of non-class type ‘Factoring()’
Problem1.InitialMessage();
The code for "FactoringProgram.h" is:
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
int InitialMessage();
};
#endif
FactoringProgram.cpp code:
#include <stdio.h>
#include <iostream>
#include "FactoringProgramH.h"
int main()
{
Factoring Problem1();
Problem1.InitialMessage();
return 0;
}
FactoringProgram2nd.cpp code:
#include "FactoringProgramH.h"
#include <stdio.h>
#include <iostream>
Factoring::Factoring(int FirstCoefficent=0, char FirstOperator='+',
int SecondCoefficent=1, char SecondOperator='+', int 3rdExpression=1)
: m_FirstCoefficent(FirstCoefficen), m_FirstOperator(FirstOperator),
m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),
m_3rdExpression(3rdExpression);
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< FirstCoefficent << "x^2 " << FirstOperator << " " << SecondCoefficent
<< " x" << SecondOperator << " " << 3rdExpression;
}
Your code has several small errors. Here is a version that works (you can compare and see what I had to change). A summary of the changes are:
default parameter values should be in the header file (*.h);
you cannot start variable/argument names with numbers;
int InitialMessage() doesn't return anything, so I changed it to void InitialMessage();
when you initialize fields in a constructor, the last field cannot end with a semi-colon;
fixed the most vexing parsing (per #RSahu comment) in main.cpp.
FactoringProgram.h
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring(int FirstCoefficent = 0, char FirstOperator = '+',
int SecondCoefficent = 1, char SecondOperator = '+', int thirdExpression = 1);
void InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
Factoring::Factoring(int firstCoefficent, char firstOperator, int SecondCoefficent, char SecondOperator, int thirdExpression) :
m_FirstCoefficent(firstCoefficent),
m_FirstOperator(firstOperator),
m_SecondCoefficent(SecondCoefficent),
m_SecondOperator(SecondOperator),
m_3rdExpression(thirdExpression)
{}
void Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
}
main.cpp
#include <stdio.h>
#include <iostream>
#include "FactoringProgram.h"
int main()
{
Factoring Problem1;
Problem1.InitialMessage();
return 0;
}
To this error substitute:
Factoring Problem1();
by:
Factoring Problem1 = Factoring();
The problem is that the compiler is interpreting this line as a function declaration rather than a variable declaration. AS mentioned in the comments, this is known as the most vexing parser problem.
Obs: The code you posted contain many more minor errors.
You should define Factoring() without params and I used headers in FactoringProgram.h
FactoringProgram.h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
Factoring(int,char,int,char,int);
int InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
int main()
{
Factoring Problem1 = Factoring();
Problem1.InitialMessage();
system("pause");
return 0;
}
FactoringProgram2nd.cpp
#include "FactoringProgram.h"
Factoring::Factoring()
{
*this = Factoring(0, '+', 1, '+', 1);
}
Factoring::Factoring(int FirstCoefficent = 0, char FirstOperator = '+',int SecondCoefficent = 1, char SecondOperator = '+', int _3rdExpression = 1) : m_FirstCoefficent(FirstCoefficent), m_FirstOperator(FirstOperator),m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),m_3rdExpression(_3rdExpression)
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
return 0;
}

g++ Undefined reference to base class destructor and derived class pointer

Edit: I had forgotten to explicitly define my class destructors in their respective .cpp files. I replaced *p with string *killList = new string[10];
and my code now compiles. Thanks for your replies!
I've tried to compile the following files using the command :
g++ -o hunter hunter_h.h hunter_h.cpp animal_h.h animal_h.cpp main.cpp
animal_h.h
#include <iostream>
#include <string>
#ifndef ANIMAL_H
#define ANIMAL_H
using namespace std;
// Animal class
class animal
{
friend class hunter;
// need a name, species, private ID
public:
animal();
animal(string aSpecies);
string name;
string species;
string getSpecies();
void setName(string aName);
string getName();
int getID();
~animal();
private:
static int uID;
};
#endif
animal_h.cpp
#include "animal_h.h"
//#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int animal:: uID = 0 ;
animal::animal()
{
cout << "Created an animal!" << endl;
name = "?";
species = "?";
uID++;
}
animal::animal(string aSpecies)
{
cout << "Created 1 "<< aSpecies << "!" << endl;
name= "?";
species = aSpecies;
uID++;
}
string animal::getSpecies()
{
cout << species << endl;
}
void animal::setName(string aName)
{
name = aName;
cout << "This " << species << " is now called " << name << endl;
}
string animal::getName()
{
cout << name << endl;
}
int animal:: getID()
{
cout << uID << endl;
}
hunter_h.h This is a derived class of the animal base class with unique behaviors.
#include "animal_h.h"
#include <iostream>
#ifndef ANIMAL_HUNTER
#define ANIMAL_HUNTER
class hunter : public animal
{
public:
hunter();
hunter(std::string aSpecies);
void recordKills(std::string kill);
static int tKills;
int totalKills();
static std::string *theKills();
static std::string *p;
static int clearTotal();
~hunter();
};
#endif
hunter_h.cpp
#include "animal_h.h"
#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int hunter:: tKills =0;
string killList[10];
hunter::hunter()
{
cout<<"created a hunter! "<<endl;
name= "? ";
species="? ";
string *p;
p = &killList[0];
}
hunter::hunter(string aSpecies)
{
name = "?";
species = aSpecies;
cout << "created a hunting "<<species <<endl;
}
string *theKills()
{
return hunter::p;
}
void hunter::recordKills(string kill)
{
cout << kill << " killed." << endl;
*(p+tKills) = kill;
tKills++;
cout << tKills << " total kills." << endl;
}
int hunter::totalKills()
{
cout << name << "'s " << "Total kills: " << tKills << endl;
}
int hunter::clearTotal()
{
delete[] killList;
return 0;
}
main.cpp
#include "animal_h.h"
#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
hunter *hunterC;
hunterC= new hunter("cheetah");
hunterC->recordKills("Mouse");
hunterC-> recordKills("Gazelle, Gazelle");
hunterC-> recordKills("Hyena");
hunterC-> recordKills("Rabbit, Rabbit");
hunterC->theKills;
hunterC->clearTotal;
}
Now, when I try to compile I get the following warning and errors:
hunter_h.cpp: In static member function ‘static int hunter::clearTotal()’:
hunter_h.cpp:49:11: warning: deleting array ‘killList’
delete[] killList;
^
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x71): undefined reference to `animal::~animal()'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x101): undefined reference to `animal::~animal()'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x119): undefined reference to `hunter::p'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x15a): undefined reference to `hunter::p'
/tmp/ccqCD1e7.o:main.cpp:(.text+0x1f4): undefined reference to `animal::~animal()'
/tmp/ccqCD1e7.o:main.cpp:(.text+0x20c): undefined reference to `animal::~animal()'
collect2: error: ld returned 1 exit status
I've been learning C++ for a couple months only so am not sure where the above code is going wrong. How can I get this to compile and run?
killList is not allocated using new[], then dont delete it with delete[]. It is not allocated runtime, then it does not have to be deallocated explicitly. It will release its memory when the program exits. With your code as it is you are running the chance of overrunning you killList array.
Try using std::vector instead.
std::vector<std::string> killList;
...
void recordKills(std::string s)
{
...
killList.push_back(s);
}
void clearTotal()
{
killList.clear();
}

Double declaration c++

So I have some code in couple of files:
cells.cpp:
#include "cells.h"
#include <iostream>
using namespace std;
char convertIntChar (int symbolNumber)
{
char charR;
switch (symbolNumber)
{
case 0:
charR='0';
break;
// lust of case code here
case 63:
charR='\\';
break;
}
return charR;
}
class cell
{
public:
int iPosition;
char chPosition;
cell ()
{
static int i = -1;
i++;
chPosition=convertIntChar (i);
iPosition=i;
cout << " " << iPosition; //two lines of code to test
cout << " " << chPosition; //constructor
}
protected:
};
main.cpp
#include <iostream>
#include "cells.h"
#include "pointer.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
createPointer();
cell cells[64];
return 0;
}
And comeplytly a cells.h
#ifndef CELLS_H_INCLUDED
#define CELLS_H_INCLUDED
#pragma once
class cell
char convertIntChar(int symbolNumber);
#endif // CELLS_H_INCLUDED
There I have an erros that sounds like
//filepath\|5|error: two or more data types in declaration of 'convertIntChar'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 7 seconds) ===|
What can it be. Sorry for noob question anyway.
First, this forward declaration needs a semi-colon:
class cell;
// ^
Second, you cannot use a forward declaration here. main.cpp needs to see the cell class definition. So you should put the definition in cells.h. For example:
cells.h:
#ifndef CELLS_H_INCLUDED
#define CELLS_H_INCLUDED
class cell
{
public:
int iPosition;
char chPosition;
cell ();
};
char convertIntChar(int symbolNumber);
#endif
cells.cpp:
#include "cells.h"
#include <iostream>
char convertIntChar (int symbolNumber)
{
char charR;
// as before
return charR;
}
cell::cell ()
{
static int i = -1;
i++;
chPosition=convertIntChar (i);
iPosition=i;
std::cout << " " << iPosition; //two lines of code to test
std::cout << " " << chPosition; //constructor
}
You have class cell in the cpp file which should go into the .h file.
Then in cells.h you are missing a ; after class cell.
Insterad of the forward declaration in cell.h, put the class there.

Linker error undefined reference to class::class (Person::Person in my case)

I am getting a linker error undefined reference to Person::Person when trying to implement my program. The three parts are below. I have been working on fixing it for a few hours now. I know it's probably something simple that I am just not seeing. But I have looked around on the internet and still have not found my answer. So any help would be appreciated.
#ifndef PERSON0_H_
#define PERSON0_H_
#include <string>
class Person // class declaration
{
private:
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person() {lname = ""; fname[0] = '\0';}
Person(const std::string & ln, const char * fn = "Hay you");
void Show() const;
void FormalShow() const;
};
#endif
#include <iostream>
#include <string>
#include "person0.h"
void Person::Show() const
{
using namespace std;
std::cout << fname << " " << lname << '\n';
}
void Person::FormalShow() const
{
using std::cout;
std::cout << lname << ", " << fname << '\n';
}
#include <iostream>
#include <string>
#include "person0.h"
int main()
{
using namespace std;
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
cout << endl;
one.FormalShow();
cout << endl;
two.Show();
cout << endl;
two.FormalShow();
cout << endl;
three.Show();
cout << endl;
three.FormalShow();
cin.get();
cin.get();
return 0;
}
I am not really a C++ person, so the terminology might be wrong, but I would say that the implementation of the
Person::Person(const std::string & ln, const char * fn)
constructor is missing.