Default constructor with no inputs - c++

I am trying to create a class which has a constructor with no input arguments - I was trying to test it by printing to the screen. However unless I give the constructor an input the constructor gets skipped straight over - the debug mode doesn't even register it as a line - can anyone shed light on this?
Also, is it possible to call the constructor of another class from inside a constructor/function belonging to a different class?
Header file:
#pragma once
#include <vector>
using namespace std;
class rain
{
public:
rain();
void update();
~rain();
private:
};
Source file:
#include "stdafx.h"
#include "rain.h"
#include "Digital Rain.h"
#include "Stream.h"
#include <Windows.h>
#include <iostream>
#include "Stream.h"
using namespace std;
int screen_width = 79;
rain::rain()
{
cout << "hi" << endl;
}
void rain::update()
{
Sleep(5);
}
rain::~rain()
{
}

Let's look at a piece of code:
#include <iostream>
class rain {
public:
rain();
};
rain::rain() {
std::cout << "hi\n";
}
int main() {
rain x();
rain y;
}
When we run this, we'll see hi on the output only once. This is because of the Most Vexing Parse.
So:
x is actually a function declaration for a function that takes no arguments and returns a rain object.
y is actually a rain object.
Additionally, your compiler might warn you about this situation. clang for example will report:
asdd.cc:26:11: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
rain x();
^~
asdd.cc:26:11: note: remove parentheses to declare a variable
rain x();
^~
1 warning generated.

Related

Class don't work in c++ project when created in other files

I made a class in a .cpp and a .h file with in the same project, and then I tried to make a calculating code. I made an object for it, with the name "co". I then tried to build it and run the file, and it showed nothing. Why is that happening and how can I try to fix it?
The code:
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "CalculatorClass.h"
using namespace std;
int main()
{
calculatorClass co ();
}
calculatorClass.cpp
#include "calculatorClass.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
calculatorClass :: calculatorClass ()
{
int x = 25;
int y = 37;
int z = 51;
int a = 14;
int b = 63;
int c = 75;
cout << x * y * z * a * b * c ;
}
calculatorClass.h
#ifndef CALCULATORCLASS_H
#define CALCULATORCLASS_H
class calculatorClass
{
public:
calculatorClass (int hello);
calculatorClass();
protected:
private:
};
#endif // CALCULATORCLASS_H
Thanks!
change calculatorClass co (); to calculatorClass co. This will work. The explanation is as the following question - C++: warning: C4930: prototyped function not called (was a variable definition intended?).
By calling calculatorClass co (); you are not creating an object; you are declaring a function.
It looks like you are trying to create a constructor.
So the first thing to do is look at your calculatorClass.h. You need to change it to look like this because a constructor is a member function that has the same name as the class.
#ifndef CALCULATORCLASS_H
#define CALCULATORCLASS_H
class calculatorClass
{
public:
calculatorClass(); //Constructor.
protected:
private:
};
#endif // CALCULATORCLASS_H
Then in your calculatorClass.cpp you will need to change it to this.The function header of a constructor's external definitions takes a form like this:
Classname::Classname(Parameters)
#include "calculatorClass.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
calculatorClass::calculatorClass() // Changed.
{
int x = 25;
int y = 37;
int z = 51;
int a = 14;
int b = 63;
int c = 75;
cout << x * y * z * a * b * c;
}
Then in your main.cpp file you are going to define an instance of the class calculatorClass which I did and named it calc. Now, the function calculatorClass is automatically called in main().
#include <iostream>
#include <fstream>
#include <string>
#include "CalculatorClass.h"
using namespace std;
int main()
{
calculatorClass Calc; // Define a calculatorClass object
return 0;
}
Just a heads up, when you use ints they only hold 32 bits. So with all these large numbers you are trying to multiplying will roll over to a large negative value and you will not get the correct answer.
You might want to use long long int.
The main issue in the code, that the following line is interpreted differently than you think:
calculatorClass calc();
This line is interpreted as a declaration of a function prototype instead of creating a new object of calculatorClass. This is known as the most vexing parse as noted in the comments.
There is a way to declare and initialize an object in a similar fashion:
calculatorClass calc{};
Notice that I have used {} and not () ! This was introduced in order to avoid this vexing parsing of the code.
In addition, I want to comment about some other bad habits in the code:
Using using namespace std; is a bad habit, which causes name pollution and may cause bugs with name ambiguity, and in the worst case related to unexpected functions being called!
Class names always start with a capital letter - calculatorClass should be Calculator (we know that it is a class, thus class shouldn't appear in the name!).
There is no need in the protected and private modifiers here.

Need help understanding object not declared in scope

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();
}

Data "member not declared in this scope"

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.

syntax error "does not name a type"

Why does my following code produce the following while compiling: error: 'Individual' in class 'Evolve' does not name a type.
#ifndef EVOLVE_H
#define EVOLVE_H
#include <cstdlib>
#include <iostream>
#include <string>
#include "Operator.h"
#include "Individual.h"
using namespace std;
class Evolve
{
public:
Evolve(int length, Operator** operators, int numOperators);
Individual* bestIndividual;
Individual* run(int generations);
Operator operatorArray[];
private:
int length;
int numOperators;
};
#endif
And my class file is
#include <cstdlib>
#include <iostream>
#include <string>
#include "Evolve.h"
#include "Operator.h"
#include "Individual.h"
using namespace std;
Evolve::Evolve(int length, Operator** operators, int numOperators)
{
Individual* bestIndividual = new Individual(length);
}
Evolve::Individual* run(int generations)
{
for(int i=0; i<generations; i++)
{
cout << "test counter = " << i << endl;
}
}
I've read a few other posts about the error and it has all been about what order to declare the function, but I'm not sure if thats the cause of my problem.
The way you implement member function is incorrect.
Update:
Evolve::Individual* run(int generations)
to:
Individual* Evolve::run(int generations)
Also, to initialize member you do not re-define it again.
Evolve::Evolve(int length, Operator** operators, int numOperators)
: bestIndividual(new Individual(length)
{
}
In your constructor,
Individual* bestIndividual = new Individual(length);
you defined a local pointer bestIndividual and it leaks memory.
The general syntax is:
return-type class-name :: function-name(arg list)
Individual* Evolve :: run (int generations)
Fix your Evolve definition

Simple function not working when using namespaces

I have the below code that compiles and executes without error, but the line that should be printed in the menu() function is never printed.
Menu.cpp
#include "stdio.h"
#include "Menu.hpp"
#include <iostream>
using namespace std;
namespace View
{
void Menu::startMenu()
{
cout << "2\n";
}
}
Menu.hpp
#ifndef MENU_H //"Header guard"
#define MENU_H
namespace View
{
class Menu
{
void startMenu();
};
}
#endif
I wrote a simple test to call the menu function, if it works correctly the output should be
1
2
3
but the 2 is never printed.
MenuTest.cpp
#include "Menu.hpp"
#include "stdio.h"
#include <iostream>
using namespace std;
int main()
{
cout << "1\n";
View::Menu startMenu();
cout << "3\n";
}
Can someone see what's going on here?
View::Menu startMenu();
Declares a function which returns View::Menu type, which is also known as most vexing parse
To initialize an object and call it's member function, you should do:
View::Menu menu;
menu.startMenu();
BTW, you need to make startMenu() function public:
class Menu
{
public: //<-----
void startMenu();
};
See live sample.
help this helps.
Because you declare function "startMenu()", that is returns type "View:Menu"
But you don't call function startMenu().
Try make following code:
View::Menu obj;
obj.startMenu();
PS. And make startMenu() as public:
class Menu
{
public:
void startMenu();
};
When substracting the brckets of View::Menu startMenu();, the code
View::Menu startMenu;
is a object definition of View::Menu, which does not call the function Menu::startMenu(). And that is when "cout << "2\n";" is not executed. To call further Menu::startMenu():
startMenu.startMenu();