considering this code i put a bp at the end of roll(int n) and i had data in values array
and i put another one at the end of print and there was no data in the array.Why do I get this error: CXX0069: Error: variable needs stack frame?
die.h
#ifndef DIE_H
#define DIE_H
#include<iostream>
#include<time.h>
using namespace std;
class Die
{
private:
int number;
int values[6][2];
void roll();
public:
Die();
void Roll(int n);
int getNumber()const{return number;}
void printLastValue();
void printApearences();
~Die(){}
};
#endif
die.cpp
#include"die.h"
#include<iostream>
#include<time.h>
using namespace std;
Die::Die()
{
srand(static_cast<int>(time(NULL)));
number=0;
for(int j=0;j<6;j++)
{
values[j][0]=j+1;
values[j][1]=0;
}
}
void Die::roll()
{
number=1+rand()%6;
}
void Die::printLastValue()
{
cout<<number<<endl;
}
void Die::Roll(int n)
{
for(int j=0;j<6;j++)
{
values[j][0]=j+1;
values[j][1]=0;
}
for(int i=0;i<n;i++)
{
roll();
(values[number-1][1])++;
}
}
void Die::printApearences()
{
for(int i=0;i<6;i++)
{
cout<<values[i][0]<<" : "<<cout<<values[i][1]<<endl;
}
}
main.cpp
#include"die.h"
#include<iostream>
using namespace std;
int main()
{
Die d;
d.Roll(5);
d.printApearences();
}
What exactly is this:
cout<<values[i][0]<<" : "<<cout<<values[i][1]<<endl;
Specifically, why are you trying to extract cout to cout? Copy/paste can be a ruthless wench. Pretty sure you want:
cout << values[i][0] <<" : "<< values[i][1] << endl;
Next, your header declarations are quite-convoluted.
Do NOT place using namespace std in any of your header files. For information on why, refer this question and its various discussions. If your fingers tire of typing std:: there are some alternatives, but in-general slurping an entire namespace (especially one as large as std) can cause unintended consequences. The linked question is worth a review.
Do not bring #include's into a header unless the content therein is dependent on it (Die.h needs nothing you're #include'ing, for example).
List your headers ahead of system headers, including in your headers, to ensure you do not code an implicit include that your header by-itself doesn't fulfill.
Include standard library C++ headers if you're compiling in C++ (use <cstdio>, <cstdlib>, <ctime>, etc.
Applying the above your code becomes:
Die.h
#ifndef DIE_H
#define DIE_H
class Die
{
private:
int number;
int values[6][2];
void roll();
public:
Die();
void Roll(int n);
int getNumber()const{return number;}
void printLastValue();
void printApearences();
~Die(){}
};
#endif
Die.cpp (top of file, code eliminated for brevity)
#include "die.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
main.cpp (top of file, code eliminated for brevity)
#include "die.h"
The last one traditionally contains <iostream> as well, but you don't actually need it in your code as it is written.
You're static cast to int for sending time(NULL) as the random seed is not correct. The srand(unsigned int seed); API takes an unsigned int as the seed, not an int. Change your seeding to be
srand(static_cast<unsigned int>(time(NULL)));
I'd start with those, specifically the first two suggestions.
It doesn't recognize rand and srand. Add #include <stdlib.h> to your die.cpp file.
The debugger will display this error when the execution is at a point where the variable in question is out of scope, and therefore cannot be evaluated. Step into your code until you reach the scope of the variable, and the debugger will show you its value.
Related
I'm trying to build a class named "Tombola" which should contain as private variable an empty vector. This should be filled at runtime through the class member Tombola.estrai(), which generates a random number and insert it inside the vector named "order" by the method order.push_back(number). This is the class definition in the tombola.h header:
#ifndef TOMBOLA_H
#define TOMBOLA_H
#include <cstdlib>
#include <vector>
using namespace std;
class Tombola {
private:
bool on_off[90];
int tabellone[9][10];
int x_max = 9;
int y_max = 10;
vector<int> order;
public:
Tombola();
~Tombola();
void nuovo();
int estrai();
bool completato();
void stampa();
void stampa_tab();
};
#endif
And this is the implementation of constructor/destructor and Tombola::estrai() inside tombola.cc:
#include "tombola.h"
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <iostream>
using namespace std;
Tombola::Tombola () {
vector<int> ord;
order = ord;
int z=1;
for(int i=0;i<90;i++) {
on_off[i] = false;
}
for(int j=0;j<=x_max;j++) {
for (int k=0;k<=y_max;k++) {
tabellone[j][k] = z;
z++;
}
}
}
Tombola::~Tombola() {
cout << "Tombola is destroyed" << endl;
}
int Tombola::estrai() {
srand(time(NULL));
int estrazione = int(ceil(rand()/double(RAND_MAX)*90));
on_off[estrazione]==true;
order.push_back(estrazione);
return order.back();
}
and this is the call to the method in the main.cpp file:
#include "tombola.h"
#include <cstdlib>
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main () {
Tombola natale;
cout << natale.estrai();
}
When I compile the program everything goes fine, but when I execute the main I get a segmentation fault error which seems to be due to some sort of allocation error when trying to store the item inside the order vector, as reported by the debugger. Could someone explain to me how to solve the error and why the error occours? Thank you.
The reason of segmentation fault is in the constructor. You have to change for(int j=0;j<=x_max;j++) to for(int j=0;j<x_max;j++) in order not to cross the bounds of the array.
for(int j=0;j<x_max;j++) {
for (int k=0;k<y_max;k++) {
tabellone[j][k] = z;
z++;
}
}
However, there are also some minor issues in the code that are worth being mentioned
declaring default-initialized ord vector and assigning it to order is pointless because order is already default-initialized.(See member initializer list for more information).
using namespace std; in a header file is a terrible idea, because if you had a large codebase, and had multiple source files where you want to include that header, everywhere the using statement will be applied, which probably is not desired.
Hey i was trying to use Separating interface from implementation but got error.
Not understanding what's wrong.
Here's my program
here's the error image
#include<iostream>
#include"name.h"
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
name n1(x,y);
n1.getdata(x,y);
n1.showdata();
}
now here's the created header file
#include<iostream>
using namespace std;
class name{
private:
int a,b;
public:
name(int x, int y);
void getdata(int x, int y);
int showdata();
};
& here's the next part of class
#include"name.h"
using namespace std;
name::name(int x, int y)
{
a=0;
b=0;
}
void name::getdata(int x,int y)
{
a=x;
b=y;
}
void name::showdata()
{
cout<<a+b;
}
There are many problems with your code. It looks like the best advice in this situation would be to read a good C++ book.
When this is out of the way, here's the short-list of problems in the severity-descending order:
name::showdata() declaration signature does not match difinitioin: int showdata() vs. void showdata()
header misses an include guard
using namespace in a header is a code smell 99 times out of 100
header does not need to include <iostream>, it would suffice to include it in implementation file, where it's actually used.
By looking at undefined references you are getting, I would also guess that name.cpp is not build.
I fixed some of the mentioned points to just make it build:
Live Demo
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.
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)
{
}
I'm having a weird problem compiling a function when I try using multiple files. I've boiled it down to this simple example: suppose I want to find the sum of a vector of integers. If I try compiling the following code, it works as expected:
#include <vector>
#include <iostream>
using namespace std;
int VectorSum (const vector<int>& values)
{
int S = 0;
for (int t=0; t < values.size(); t++)
{
S += values[t];
}
return S;
}
int main()
{
vector<int> values;
values.push_back(-100);
values.push_back(75);
values.push_back(75);
cout << "Total = " << VectorSum(values) << endl << endl;
cin.ignore(1, '\n');
return 0;
}
However, if I try using a header file, it crashes on my (error C4430 when compiling on VS 2010 for Windows XP). Here's the code for the other approach:
the header:
/* VectorSum.h */
#pragma once
#include <vector>
int VectorSum (const vector<int>& values);
the source:
/* VectorSum.cpp */
#include "VectorSum.h"
#include <vector>
int VectorSum (const vector<int>& values)
{
int S = 0;
for (int t=0; t < values.size(); t++)
{
S += values[t];
}
return S;
}
the implementation:
/* Main.cpp */
#include "VectorSum.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> values;
values.push_back(-100);
values.push_back(75);
values.push_back(75);
cout << "Total = " << VectorSum(values) << endl << endl;
cin.ignore(1, '\n');
return 0;
}
As you can see, the code for the function in VectorSum.cpp is identical to the code in my first .cpp file, so the problem must be in the header. Any ideas what I'm doing wrong?
#pragma once
#include <vector>
int VectorSum (const std::vector<int>& values);
^^^^^
See the MSDN page for C4430. It is issued in case when a declaration is missing a type or the type is unknown. In your case vector is an unknown type due to unqualified name lookup rules.
It's an issue with the std namespace.
Change the declaration in the header to:
int VectorSum (const std::vector<int>& values);
And make sure there's either a using namespace std; in the .cpp file(s) (like your first example) or that you use the std namespace appropriately when calling/defining the function. For example, you'd need to do one of these things in your VectorSum.cpp file.
As an aside, please don't add a
using namespace std;
statement to the header file. That will force the namespace to be brought into scope for all users of the header (even if it's indirectly included, so it might not be obvious), which might not fit their wants or needs.
The problem is indeed in the header file. You forgot to add
using namespace std;
to the header and thus the compiler doesn't know what vector means.
Corrected header:
/* VectorSum.h */
#pragma once
#include <vector>
using namespace std; // This was missing
int VectorSum (const vector<int>& values); // Now OK, the compiler knows vector
You have to specify the namespace for the vector in VectorSum.h:
int VectorSum (const std::vector<int>& values);
Add a using namespace std
/* VectorSum.h */
#pragma once
#include <vector>
<--------
//using namespace std;
int VectorSum (const std::vector<int>& values);
Try to avoid using namespace in header files to avoid name conflicts.