I was given and assignment to develop a cellphone simulation, part of the requirements includes me using a queue to store the phone numbers that come from a txt file to simulate getting called. I have reviewed over and over again and including #include but the error message telling me "Queue" undeclared still shows up. I am using Bloodsehd Dev-C++, and I have included a sample I used to test the queue, if it is just the program not having the right information, any help would be appreciated. I have checked other sites most say add a #include or a #include neither seemed to solve the problem.
#include <queue>
#include <deque>
#include <iostream>
#include <fstream>
using namespace std;
class cell
{
public:
cell();
~cell();
void upload( string item );//Insert in order
void printnew();//Print call just recieved
void printlater();//Print calls for later
int Front(); //returns front element
int Rear(); //returns rear element
bool Empty();
private:
};
int main()
{
Queue Q;
Q.Enqueue(54);
cout << Q.front();
system("Pause");
return 0;
}
To use STL queue, you need to:
#include <queue> // include necessary header
std::queue<int> q; // initialize queue container with type int
q.push(54); // add element to queue
std::cout << q.front() << std::endl; // access the head of queue
checkout std::queue reference http://en.cppreference.com/w/cpp/container/queue
If you want to use C++ Standard Library queue, you must declare it as
queue<int> Q;
Q.push(54);
cout << Q.front();
queue is supposed to be lowercase.
You can find info on using the queue here:
http://www.cplusplus.com/reference/queue/queue/queue/
Related
OK so here's my question, I'm trying to understand the use of friend in C++ with a working example on my PC for reference. I have everything set up in different classes, which are connected to one another with the .h files etc. (I think anyways). Can someone show me where I'm going wrong please? Cause i keep getting compiler errors and I don't understand what it means.
Yes I've researched the C:xxxx errors online, but I can't link the problem to the code I've got... It's obviously wrong! just need a push in the right direction / better understanding of C++... thank you!
//First class
#include <iostream>
#include <string>
using namespace std;
class Tutorial48
{
private:
int var;
int secret = 5;
public:
Tutorial48(int v);
friend class Tutorial48UseFriend;
void PrintVar();
};
// First class .cpp
#include "Tutorial48.h"
Tutorial48::Tutorial48(int v)
{
var = v;
}
void Tutorial48::PrintVar()
{
cout << var << endl;
cout << "Scret variable = " << secret << endl;
}
// Second class, whole point is to demo the friend keyword in C++!
#include "Tutorial48.h"
#include <iostream>
#include <string>
using namespace std;
class Tutorial48UseFriend
{
public:
Tutorial48UseFriend();
void showSecret(Tutorial48 tut48F)
{
// Just trying to increment by 1 so i know it's worked correctly.
tut48F.secret++;
cout << "My new private variabe = " << tut48F.secret << endl;
};
};
// Main function for outputting the whole thing.
#include "Tutorial48.h"
#include "Tutorial48UseFriend.h"
#include <string>
#include <iostream>
int main
{
Tutorial48UseFriend fr;
Tutorial48 t(24);
fr.showSecret(t);
getchar();
return 0;
}
Errors being produced....
Errors
Yeah so that's everything... As i said i'm new to this, so trying to understand it. Thank you to anyone for help in advance, cheers guys.
P.s. I do kind of understand the concepts of friend in c++ how it's used to access private variables of other classes etc. but i have no idea how to code it properly...
You should define the constructor of Tutorial48UseFriend like:
Tutorial48UseFriend(){}
and also pass Tutorial48 object by reference (by using '&'), and instead of making the function void make it return an int and then print it later, so:
int showSecret(Tutorial48& tut48F)
{
return ++tut48F.secret;
}
#include <iostream>
#include <queue>
using namespace std;
int main () {
struct process {
int burst;
int ar;
};
int x=4;
process a[x];
queue <string> names; /* Declare a queue */
names.push(a[1]);
return 0;
}
I'm trying to pushing struct variable in queue but its not taking it and gives errors
no matching function for #include queue and invalid argument
how can I do that?
C++ is a strongly typed language. In the line names.push(a[1]); you are trying to push a struct (from your process a[x]; array) into a queue<string>. Your struct is not a string, so the compiler will emit an error. You at least need a queue<process>.
Other issues: variable length arrays are not standard C++ (process a[x];). Use a std::vector<process> instead. Here is some simple example that works:
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main () {
struct process // move this outside of main() if you don't compile with C++11 support
{
int burst;
int ar;
};
vector<process> a;
// insert two processes
a.push_back({21, 42});
a.push_back({10, 20});
queue <process> names; /* Declare a queue */
names.push(a[1]); // now we can push the second element, same type
return 0; // no need for this, really
}
EDIT
Locally defined classes/structs used to instantiate templates are valid only in C++11 and later, see e.g. Why can I define structures and classes within a function in C++? and the answers within. If you don't have access to a C++11 compliant compiler, then move your struct definition outside of main().
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am new to C++ programming but I know that pointers cause segmentation error. The problem is in the Readline() method when I am trying to read a sudoku but I cannot fix it. What am I missing?
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include "Sudoku.h"
using namespace std;
// Constructor
Sudoku::Sudoku(){
root=cells;
row=0;
row_ptr=&row;
}
void Sudoku::Readline(string s,int i) {
int lead;
for(int k=0;k<9;k++){
lead=(9*i)+k;
if (s[k]!=',') {
*(root+lead)=s[k];
} else {
*(root+lead)=0;
}
}
}
void Sudoku::MakeSudoku(string s){
//cout<<(*row_ptr)++<<' '<<s<<"\n";
Readline(s,(*row_ptr)++);
}
The class definition is
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Sudoku{
public:
int cells[81];
int row;
int *root;
int *row_ptr;
public:
Sudoku();
void MakeSudoku(string s);
void Readline(string s,int i);
void PrintSudoku();
};
The main file is
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "Sudoku.cpp"
using namespace std;
int main()
{
Sudoku sd;
// Input csv file containing sudoku
ifstream filen("sudoku.csv");
string s;
if(!filen.is_open()){
cout << "Error opening file";
} else{
while(!filen.eof()){
getline(filen,s);
sd.MakeSudoku(s);
}
}
filen.close();
//sd.PrintSudoku();
return 0;
}
Your code is no C++ code. Except file operations it is (bad styled) C code. You are using a plain array (cells), you even do an unnecessary copies of the array (root) and that pointer arithemtic is dangerous (as you are currently experiencing).
I think you should rewrite your code a bit which will solve your problem:
You should use descriptive variable names... k,s,i,etc. are hard to read
Use a two-dimensional array for 'cells'. Or even better a C++ container like a vector of vectors. The latter would check boundaries and you could get rid of your pointer arithmetics (which causes such faults when done the wrong way) and you could use plain indexes.
Use proper indentions and empty lines for block separation
Don't use magic numbers like "81" and "9". Create constants. Give them names! make them dependent from each other if they are dependent.
I am trying to create a lottery program within c++ and the issue i'm having is attempting to output all values of the Numbers array into a file, however when i run the code, the only thing that gets outputted is the first set of values i put in, however the program allows me to type in more than one set. (The program allows for up to 6 sets of data), however it only outputs one.
Here is all my code
LotteryData.cpp
LotteryData::LotteryData()
{
}
LotteryData::~LotteryData()
{
}
void LotteryData::PassInfo(int (&Numbers)[6][6], int &NumberofGames)
{
ofstream Numfile;
while(NumberofGames>0)
{
Numfile.open("Numbers.txt");
for (int j=0; j<6; j++)
{
Numfile << Numbers[NumberofGames][j];
}
NumberofGames = NumberofGames - 1;
Numfile.close();
}
}
Player.h
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class Player
{
private:
public:
Player();
~Player();
void Input();
int Numbers[6][6];
int NumberofGames;
};
main.cpp
#include <iostream>
#include "Lottery.h"
#include "Player.h"
#include "LotteryData.h"
using namespace std;
int main()
{
Player player;
Lottery random;
LotteryData data;
player.Input();
random.setRandomNumber();
data.PassInfo(player.Numbers, player.NumberofGames);
}
Im not exactly sure where the problem is coming from but i think it may be from one of the pointers although not entirely sure.
Any help on this problem would be much appreciated.
Cheers
Edit: I've Changed the code within the PassInfo function within LotteryData.cpp file as #ali suggested
Edit2: I've cut down on the code as to where I think the problem occurs but as all of the code compiles, Visual Studio 2012 doesnt point to any actual errors in the program
In the while loop in the PassInfo function you are not changing the value of NumberOfGames. Therefore in the for loop you are using the same row of your Numbers array. You need another for loop to change the index for the first index of the Numbers[][] array.
I am working on a project using multimaps inside of my own class, and I have run into a segfault. Here are the parts of my code relating to the issue. I would really appreciate some help. Thanks.
Here is database.h
#include <iostream>
#include <map>
using namespace std;
class database{
public:
database(); // start up the database
int update(string,int); // update it
bool is_word(string); //advises if the word is a word
double prox_mean(string); // finds the average prox
private:
multimap<string,int> *data; // must be pointer
protected:
};
Here is the database.cpp
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include "database.h"
using namespace std;
// start with the constructor
database::database()
{
data = new multimap<string,int>; // allocates new space for the database
}
int database::update(string word,int prox)
{
// add another instance of the word to the database
cout << "test1"<<endl;
data->insert( pair<string,int>(word,prox));
cout << "test2" <<endl;
// need to be able to tell if it is a word
bool isWord = database::is_word(word);
// find the average proximity
double ave = database::prox_mean(word);
// tells the gui to updata
// gui::update(word,ave,isWord); // not finished yet
return 0;
}
Here is test.cpp
#include <iostream>
#include <string>
#include <map>
#include "database.h" //this is my file
using namespace std;
int main()
{
// first test the constructor
database * data;
data->update("trail",3);
data->update("mix",2);
data->update("nut",7);
data->update("and",8);
data->update("trail",8);
data->update("and",3);
data->update("candy",8);
// cout<< (int) data->size()<<endl;
return 0;
}
Thanks very much. It compiles and runs up to cout << "test1" << endl; but segfaults on the next line.
Rusty
You never actually created a database object, just a pointer to nowhere (perhaps you're used to another language).
Try creating one like this database data;
And then change your -> to . to access the members.
Consider obtaining one of the books at The Definitive C++ Book Guide and List as well.
You need to allocate your database before starting to insert data on it.
Change:
database *data;
to:
database *data = new database();
or:
database data;
in main().
EDIT: if you use the latter, change -> to . on the subsequent method calls. Otherwise, remember to delete your data object after using it.