Segmentation Fault C++ pointers [closed] - c++

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.

Related

Error message when creating a vector of objects [closed]

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 4 months ago.
Improve this question
for a homework i have to create a vector of objects who have some attributes, included an ip adress, and order the vector according to their IP adress. I'm having some trouble creating the vector, heres the code.
#include <iostream>
#include <fstream>
#include <string>
#include <string>
#include <algorithm>
#include <vector>
#include "data.h"
using namespace std;
void createVect(ifstream inFile) {
string month, day, ip1, ip2, ip3, ip4, temp, ip;
string t1;
int i = 0;
size_t n = 3;
vector<data> vect;
}
but im getting an error when trying to define the vector: "data" is ambiguousC/C++(266).
The code is bigger but this is the only relevent part (I think).
class data{
public:
std::string mes;
int dia;
std::string hora;
int ip;
data();
};
It's a pretty simple class, i just need it to store the data from the file i'm using.

Undefined reference to `WinMain' in C++ while exceuting on vectors

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.

All strings are unidentified

#include <iostream>
#include <time.h>
#include <string.h>
#include <stdio.h>
int main()
{
string msg;
printf("Enter the message that you wish to display as scroller: ");
getline(cin,msg);
msg=msg+". ";
int x=0;
while(1)
{
Scroll(msg);
wait(100);
system("cls");
x++;
}
cin.get();
return 0;
}
I Have this C code and all strings in the file say 'identifier "string" is undefined'. I tried including <string> instead of <string.h> but it didn't work. Why is it not working?
Add
using namespace std;
After includes (but before main). Or, better, use notion of:
std::string // instead of string
Update: I missed the point of this being C-question. I will leave this answer, but for the sake of formality, use it if you came from Google and you are working with C++.
This is C++ code, not C.
The compiler is probably getting confused because it cannot parse it, so then it finds C-like code and all identifiers do not exist.
The includes should be:
#include <iostream>
#include <ctime>
#include <string>
#include <cstdio>
You are also missing a:
using namespace std;
Plus the definitions for Scroll and wait etc.

Can't read object in vector in C++ [closed]

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 6 years ago.
Improve this question
I have a problem i don't understand why I can't get my value from a vector :
And I think the error is i about how i use my vector.
i have 3 files :
the Header of my class Group
Group.hpp
#ifndef Group_hpp
#define Group_hpp
#include <iostream>
#include <string>
#include <vector>
#include "Etapes.hpp"
using namespace std;
class Group{
float coefficiant;
int note;
public:
Group(float coefficiant,int note);
float getCoefficiant();
int getNote();
};
#endif /* Group_hpp */
Group.cpp (where I defined the content of my class)
#include "Group.hpp"
Group::Group(float coefficiant,int note){
this->coefficiant = coefficiant;
this->note = note;
}
float Group::getCoefficiant(){
return this->coefficiant;
}
int Group::getNote(){
return this->note;
}
and the main : Where I execute my class.
#include <iostream>
#include "Etapes.hpp"
#include "Group.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
vector<Group> listGroup;
listGroup.push_back(*new Group(2.2,5));
for(int i = 0;i<listGroup.size();i++){
cout<<listGroup[i].getCoefficiant()<<endl;
}
return 0;
}
I am really lock on this class.
Thank you
Instead of
listGroup.push_back(*new Group(2.2,5));
just use
listGroup.push_back(Group(2.2,5));
One of the big advantages of STL containers it that they encapsulate dynamic memory allocation.

Redundant namespace declaration vector [closed]

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 6 years ago.
Improve this question
I'm new to c++ so there are tons of things I don't know, that's why I would like to ask someone with more experience.
std::vector<CProp*> filter(const string &deptName, const string &city, const string &country)const {
...
}
I'm using std namespace, so the std:: should be redundant, but if I remove it, the compiler shows errors (first of which is This declaration has no storage class or type specifier?). Why is that? I never had to use it elsewhere in the class, so there shouldn't be any conflict also I'm using only std namespace.
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <memory>
#include <vector>
#include <algorithm>
using namespace std;
class ClassName {
public:
...
private:
vector<CProp*> vector;
vector<CProp*> filter(const string &deptName, const string &city, const string &country)const {
return nullptr;
}
}
This defines a member named "vector" which conflicts with std::vector
private:
vector<CProp*> vector;
string also requires std::. So you should have
std::vector<CProp*> filter(const std::string &deptName, const std::string &city, const std::string &country)const {
...
}
And I agree with all the commenters saying "Don't use using namespace std".
You didn't close your class declaration with a semicolon ;. That is confusing the compiler.
You also need to write void SomeFunctions(); as that is confusing the compiler too. And don't forget to add a definition for that function otherwise the link stage of your build will fail.
You'll also need some way of running something. To do that you need a main function. Or is that the job of someone else?