I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.
Related
So I have two classes - Dvd and DvdGroup. DvdGroup basically manages an array of dvds and provide manipulative member functions for that class. The problem is whenever I try to compile DvdGroup.cc using the command 'g++ -c Dvd.Group.cc', I get a bunch of errors all related to not having 'Dvd' declared and I'm not sure why.
Here are some errors below:
DvdGroup.h:14:12: error: ‘Dvd’ has not been declared void add(Dvd*);
DvdGroup.h:18:3: error: ‘Dvd’ does not name a type Dvd* dvdCollection[MAX_DVDS];
DvdGroup.cc: In copy constructor ‘DvdGroup::DvdGroup(DvdGroup&)’:
DvdGroup.cc:15:6: error: ‘Dvd’ was not declared in this scope for(Dvd d: dvds){
I feel like I'm missing something and they could all be fixed by one solution because they all involve having the Dvd class undeclared but I can't seem to figure out what. I was wondering if anyone could tell me what I'm doing wrong? I would really appreciate any help with fixing this.
DvdGroup.cc:
#include <iostream>
using namespace std;
#include "DvdGroup.h"
DvdGroup::DvdGroup(int n){
numDvds = n;
}
DvdGroup::DvdGroup(DvdGroup& dvds){
numDvds = dvds.numDvds;
for(Dvd d: dvds){
Dvd newDvd = Dvd;
}
}
DvdGroup::~DvdGroup(){
//code
}
void DvdGroup::add(Dvd* d){
//code
}
DvdGroup.h:
#ifndef DVDGROUP_H
#define DVDGROUP_H
#define MAX_DVDS 15
#include <string>
using namespace std;
class DvdGroup
{
public:
DvdGroup(int);
DvdGroup(DvdGroup&);
~DvdGroup();
void add(Dvd*);
private:
Dvd* dvdCollection[MAX_DVDS];
int numDvds;
};
#endif
Don't know if the Dvd header file is needed, but here:
Dvd.h:
#ifndef DVD_H
#define DVD_H
#define MAX_DVDS 15
#include <string>
class Dvd{
public:
Dvd(string, int);
void set(string, int);
Dvd(Dvd&);
int getYear();
~Dvd();
void print();
private:
string title;
int year;
};
#endif
What you need to do is to provide Dvd class definition for DvdGroup class. It is needed to know what type of symbol is this. Solution for your problem should be addition of:
#include "Dvd.h"
line to DvdGroup.h file.
I wrote down an example code to try to replicate the error I am getting in a school project about the scope of an object:
In file: classTest.cpp
#include "headerone.h"
#include "headertwo.h"
#include <iostream>
using namespace std;
int main() {
ClassOne* pntrObj1 = new ClassOne;
ClassTwo* pntrObj2 = new ClassTwo;
pntrObj1->testClassOne();
return 0;
}
In file: headerone.h
#ifndef HEADERONE_H
#define HEADERONE_H
#include "headertwo.h"
#include <iostream>
using namespace std;
class ClassOne {
public:
void testClassOne() {
cout << "One Worked\n";
pntrObj2->testClassTwo();
}
};
#endif
In file: headertwo.h
#ifndef HEADERTWO_H
#define HEADERTWO_H
#include <iostream>
using namespace std;
class ClassTwo {
public:
void testClassTwo() {
cout << "Two Worked";
}
};
#endif
To be clear, the error is: pntrObj2 was not declared in this scope. The error comes from the file headerone.h
If I had to guess, I need to somehow pass the reference but I am not sure where to start for that. Any help is appreciated.
The variable pntrObj2 is only visible inside the scope in which it was declared, in this case your function main(). In other words, only code inside the curly braces of main() would be able to use the name pntrObj2 to reference that variable. However you can pass that value to other pieces of code by making it the argument of a function call.
So maybe what you want to do is add an argument to the testClassOne() method, so you can pass in the value of pntrObj2. So pntrObj1->testClassOne(); would become pntrObj1->testClassOne(pntrObj2);, and where you define testClassOne you can add a corresponding parameter. I'll let you figure this out so as to not completely do your homework for you :)
Here you include your file a lot of time and in testClassOne function, you do not declare pntrObj2
use
void testClassOne() {
cout << "One Worked\n";
ClassTwo* pntrObj2 = new ClassTwo()
pntrObj2->testClassTwo();
}
insteed of
void testClassOne() {
cout << "One Worked\n";
pntrObj2->testClassTwo();
}
I have a programming assignment where I'm supposed to write up the code for inserting and removing linked lists. However I haven't used C++ in a while and am struggling remember certain things.
Right now, I am simply trying to put a prototype method in a header file, define it in my cpp file, and then call it in my main method. this is what I have.
LinkedList.h
#include <iostream>
using namespace std;
class LinkedList {
public:
void testPrint();
};
LinkedList.cpp
#include "LinkedList.h"
int main() {
LinkedList::testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
I am getting the following errors
a nonstatic member reference must be relative to a specific object
'LinkedList::testPrint': non-standard syntax; use & to create a pointer to member
LinkedList::testPrint() is a member function.
It is not declared static, so that means it must be called on a particular object, defined as LinkedList linked_list, for example. Then use linked_list.testPrint().
Option 1 - static member function declaration
#include <iostream>
using namespace std;
class LinkedList {
public:
static void testPrint();
};
int main() {
LinkedList::testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
Option 2 - Instantiated object with call to member function
#include <iostream>
using namespace std;
class LinkedList {
public:
void testPrint();
};
int main() {
LinkedList linked_list;
linked_list.testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
I'm seriously confused why this is happening. I get an error 'enzyme_acronyms_ was not declared in this scope'. It points to my writeAcronym function but not getAcronym, and both use enzyme_acronyms_. What can possibly cause this?
SequenceMap.h
#ifndef SequenceMap_h
#define SequenceMap_h
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class SequenceMap
{
private:
string recognition_sequence_;
vector<string> enzyme_acronyms_;
public:
string getAcronym();
void writeAcronym(string an_enz_acro);
}
SequenceMap.cpp
#include "SequenceMap.h"
string SequenceMap::getAcronym()
{
return enzyme_acronyms_[0]; //works fine
}
void writeAcronym(string an_enz_acro)
{
enzyme_acronyms_.push_back(an_enz_acro); //enzyme_acronyms_ not declared in this scope
}
You've missed the SequenceMap:: qualification on the second function definition:
void SequenceMap::writeAcronym(string an_enz_acro)
It must be declared like this:
void SequenceMap::writeAcronym(string an_enz_acro)
{
enzyme_acronyms_.push_back(an_enz_acro);
}
You forgot the class scope SequenceMap::.
I am farily new to C++ and I have been stuck with this problem for a few hours now. I am trying to setup the foundations for a video game related experience calculator, but I can't get past this problem.
main.cpp
#include <iostream>
#include "Log.h"
using namespace std;
int main()
{
Log Logs;
enter code here
struct ChoppableLog Yew;
Logs.initialiseLog(Yew, 60, 175);
return 0;
}
Log.h
#ifndef LOG_H
#define LOG_H
struct ChoppableLog
{
int level;
int xp;
};
class Log
{
public:
void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int);
Log();
};
#endif // LOG_H
Log.cpp
#include "Log.h"
#include <iostream>
using namespace std;
Log::Log()
{
}
void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{
}
The error I get is
C:\Users\Murmanox\Documents\C++\C++ Projects\CodeBlocks\Class Files Test\main.cpp|11|undefined reference to `Log::initialiseLog(ChoppableLog&, int, int)'|
I can post more details if necessary.
You have to define Log::initialiseLog with its full name, like so:
void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{ }
What you are doing is defining a new, free function of the name initialiseLog instead of defining the member function of Log.
This leaves the member function undefined, and, when calling it, your compiler (well, technically linker) will be unable to find it.
The definitions of functions in a header file should specify the scope. In your case, you should define initialiseLog() function in your cpp file as follows:
void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{
}