for some reason I get the "Process terminated with status -1073741819" error whenever I run my program, I've read that some people get this error because of something wrong with code-blocks/the compiler, i just wanted to know if there is anything wrong with my code before i go reinstalling compilers and such. I'm using code::blocks and the GNU GCC compiler.
my code creates a vector which stores 40 working hours in a week, and a vector inside that vector which stores letters representing the 5 people available in those hours.
Schedule.cpp:
#include <iostream>
#include "Schedule.h"
#include <vector>
#include <string>
using namespace std;
/// Creates a Vector which holds 40 items (each hour in the week)
/// each item has 5 values ( J A P M K or X, will intialize as J A P M K)
vector< vector<string> > week(40, vector<string> (5));
Schedule::Schedule(){
for (int i = 0; i<40; i++){
week[i][0] = 'J';
week[i][1] = 'A';
week[i][2] = 'P';
week[i][3] = 'M';
week[i][4] = 'K';
}
// test
cout << week[1][3] << endl;
}
header file:
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include <vector>
#include <string>
using namespace std;
class Schedule
{
public:
Schedule();
protected:
private:
vector< vector<string> > week;
};
#endif // SCHEDULE_H
main.cpp:
#include <iostream>
#include "Schedule.h"
#include <vector>
#include <string>
using namespace std;
int main()
{
Schedule theWeek;
}
This is not a copiler bug.
You are getting a memory fault in your constructor.
There are several things wrong with your code, for example in your cpp you declare a global vector week which then is hiden in the constructor since the constructor will access Schedule::week .
Your cpp should be something like :
// comment out the global declaration of a vector week ...
// you want a vector for each object instantiation, not a shared vector between all Schedule objects
// vector< vector<string> > week(40, vector<string> (5));
Schedule::Schedule()
{
for (int i=0;i<40;i++)
{
vector<string> stringValues;
stringValues.push_back("J");
stringValues.push_back("A");
stringValues.push_back("P");
stringValues.push_back("M");
stringValues.push_back("K");
week.push_back(stringValues);
}
}
You get the memory fault in your code when you try to access your week vector for the first time :
week[i][0] = 'J' ;
At the moment you call that line of code, your Schedule::week vector has 0 elements inside it (so week[i] is already a fault).
Related
I'm having trouble compiling code with a struct vector. The compiler keeps sending errors, but I cannot locate any. The code is right beneath.
//2018 USACO Bronze Task 2
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
struct Period{
int end,starting;
};
int main(){
ifstream fin("lifeguards.in");
ofstream fout("lifeguards.out");
int N;
fin>>N;
vector <Period> periods[N];
for(int i=0;i<N;i++){
fin>>periods[i].starting>>periods[i].end;
}
int record=0,temp=0;
for(int i=0;i<N-1;i++){
temp+=periods[i].end-periods[i].starting;
for(int j=0;j<N;j++){
if(j==i)continue;
temp+=periods[j].end-periods[j].starting;
temp-=max(periods[i].end-periods[j].starting,0);
}
if(temp>record)record=temp;
temp=0;
}
fout<<record<<endl;
}
The error message is in the link
Errmsg
I have checked for any possible grammatical errors that I know of, but it keeps popping out compilation errors. Is there any fix to this?
lets look at this line:
vector <Period> periods[N];
in this line you defined array of vector so when you call periods[i] this will return one of array item which is vector <Period> so this is returned type not an Period struct it is vector type so it is incorrect. in order to achieve this you should call periods[i][j] or if you mean you want vector with N size you can call reserve method.
I have to calculate the longest prefix string in the program. I am using c++ for this and I don't actually know extensively about vector and its functions.
The code is:
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
class Solution {
public:
void longestCommonPrefix(vector<string>& strs)
{
string ans;
strs.size(); //no of rows
strs.push_back("flower");
strs.push_back("flower");
string one=strs[0];
string two=strs[1];
int oneL=one.length();
int twoL=two.length();
int min=oneL<twoL?oneL:twoL;
for(int i=0;i<min;i++)
{
char temp=one[i];
if(temp==two[i] )
ans=ans+temp;
}
}
};
This displays an error of winMain and I do understand it must be related to the main function but the problem is I cannot put nain() function here else it displays an error again. Thus, I cannot put a main function and I if I don't I face an error.
Help me out stackmates.
This code at the indicated lines are both producing the
error: Get is not a member of 'std'
Additionally the code also does not accept .first and .second notation and throws the error that they aren't declared in this scope.
#include <string>
#include <iostream>
#include <fstream>
#include <iterator>
#include <map>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
...
std::pair <int,int> pairs;
queue <int> quads;
map<int,std::pair<int,int> > pairSets;
...
int parseValue(int searchVal)
{
int tempVal;
for(int i = 0; i < count; i++)
{
for(int j = 1; j < count; j++)
{
tempVal = data[i]+data[j];
if(pairSets.find(searchVal-tempVal) != pairSets.end())
{
quads.push(data[i]);
quads.push(data[j]);
quads.push(std::get<0>(pairSets.find(searchVal-tempVal))); //These lines are producing errors
quads.push(std::get<1>(pairSets.find(searchVal-tempVal)));
}
}
}
}
Unfortunately i am not as familiar with c++ programming than i am a with C#, i am struggling to find the reason for this error through the C++ source code, i believe i am using maps and pairs incorrectly but i can't find any resources to indicate in what way i am misusing them.
I would appreciate any help or advice anyone has to offer, thank you.
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.