Can't read object in vector in C++ [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 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.

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.

C++ use of undeclared identifier [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I know there's a lot of different threads with this similar issue but I've gone through a number of them and have fixed any of the possible issues that may be resulting in this error but I keep getting it.
I'm trying to test some of functions in my class file but whenever I try calling them I get that error.
Here's one for example.
main.cpp
#include <iostream>
#include <vector>
#include "Graph.h"
using namespace std;
int main(){
Graph graph;
newState();
return 0;
}
Graph.h (I know I shouldn't be using namespace here but I'm still doing it for now)
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
class Graph{
public:
Graph();
Graph(vector<Edge> const &edges, int N);
void printGraph(Graph const &graph, int N);
// print adjacency list representation of graph
void createTree(GraphNode *root, int state[]);
void newState();
private:
int states = 8;
int initialState[8];
int nextState[8];
};
Graph.cpp
#include "Graph.h"
void Graph::newState(){
srand (time(NULL));
int random = rand() % 4 + 1;
cout << random;
};
I feel like I'm trying to do the bare minimum but it just does not want to work. Can anyone please tell me where I'm going wrong?
newState is not a free-standing function. It is a non-static member function of Graph class. Which means, it must be called on an instance of Graph. What you probably meant to do is:
Graph graph;
graph.newState();

In c++ if header is included why I got 'does not a name of type' error? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I have data.h:
#ifndef DATA_H_INCLUDED
#define DATA_H_INCLUDED
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class student{
public:
string id;
int points [6] = {0,0,0,0,0,0};
};
#endif // DATA_H_INCLUDED
And I have enor.h:
#ifndef ENOR_H_INCLUDED
#define ENOR_H_INCLUDED
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include "data.h"
using namespace std;
enum status{norm,abnorm};
class enor{
public:
/*some voids*/
Student Current() const { return elem; }
student elem;
private:
/*some voids*/
};
#endif // ENOR_H_INCLUDED
And I got 'Student' does not a name a type, but why? I tried also if the Student calss is in enor.h, but also this error. how can I resolve this, and why is this?
You have a difference in your case for your student class:
class student - Lower case s
Student Current() const - Upper case S

Random consts on initialization lists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
There seems to be an issue when I try to make initialization list with a const number while its a random number, cant realy figure out the problem and didnt found a proper solution when I use random const's on initizilation lists.
die.h
#ifndef die
#define die
#include <iostream>
#include "time.h"
#include "stdlib.h"
class die{
private:
const int dieFaces;
public:
die();
// Getters
int getFaces();
// Common Functions
void printDie(die);
void roll();
int copyConstructor(die);
// Destructors
~die(){};
};
#endif die
die.cpp
#include "die.h"
#include <iostream>
#include "time.h"
#include "stdlib.h"
#include <random>
using namespace std;
// Constructor
die::die() : dieFaces(rand() % 20 + 1){};
the error i'm getting is "Declartion does not declare anything" while on the task I was asked to create an empty constructor.
and in the CPP file it seems to expect all sorts of ";" and "Declartion of Variable expected"...
Any help will be appreciated. thanks.
Don't use the include guard die: it's the same as the class name.
The preprocessor will substitute empty text every time it sees the string die. The compiler will see
class {
private:
etc., which is not compilable.
Use something like #define included_die_hpp instead.

Segmentation Fault C++ pointers [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 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.