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.
Related
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 6 years ago.
Improve this question
I am trying to create a code using vectors and other c++11 utilities. The above mentioned(on the title) error occurs in my code and despite I looked for a solution to this error on the internet I did not find something that works into my code. I tried to make some type castings but did not work. I present you the contentious part of code below:
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
#include <algorithm>
#include <list>
//#include <Winbase.h>
using namespace std;
// A struct describing a product.
typedef struct Products
{
string category;
string name;
float price;
} Product;
inline void scenario1(int num_cashiers)
{
vector<Product> products; // It is a vector(a pseudo-second dimension) of products which will be used for each customer
vector<vector<Product>> customers; // A vector containing all customers
vector<vector<vector<Product>>> cashiers(num_cashiers); // A vector describing the supermarket cashiers declaring a queue of customers for each cashier
double start = GetTickCount(); // It will be used for counting 10 secs until next update
vector<int> total_products(num_cashiers); // A vector keeping the total number of products of each queue
list<string> categories; // A list containing all the categories of the products
list<float> categories_prices(categories.unique().size()); // A list containing all category prices
//THE ABOVE LINE - THE LAST ONE IN THIS PART OF CODE - IS THE LINE I GET THE ERROR!!!
....
}
What is wrong with the code?
Thank you everyone in advance!
list::unique is a void function, it does not return anything. On that last line, where you call categories.unique().size(), you are calling .size() on a void expression.
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
My previous post has already been tagged as a duplication - https://stackoverflow.com/questions/36960042/lots-of-unreasonable-compiler-errors-c
Ive tried the suggested solutions.
However when forward declaring "class course;", my compiler doesnt seem to recognize the class in the previous files, saying that course(the class) is an incomplete type in every place its mentioned in "student.cpp".
Did I miss the point? how do I resolve the circular dependancy in my code?
(Code in previous post).
"course" is tagged as incomplete in student.cpp
"student.h" -
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
class course;
class student{
private:
string name;
int id;
string gender;
int age;
public:
int amountofcourses;
student();
~student();
course **courses;
};
"student.cpp" -
#include "student.h"
student::student(){
courses = NULL;
course *courses = new course;
}
"course.h" -
#include "student.h"
#pragma once
class course{
private:
string name;
int num;
int amountofstudents;
public:
course();
~course();
Just delete #include "course.h" from student.h, where you don't need it and it is causing a circular dependency, and add it to student.cpp where you actually do need it.
EDIT: Note that this answer was written before the posted code was edited to match half of what I suggest here.
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.
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 am trying to make a library system using C++, where i would ask the user for input for book name, author, and year and it will be stored in a linked list. I have done the following so far in the header file (which seems to have no errors)
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
class LinkedList {
private:
struct BookNode {
string Book_Title;
string Author_Name;
int Year_of_Publishing ;
BookNode* next;
};
public:
LinkedList();
void addInfo(string,string, int);
void print();
};
and for the .cpp
void LinkedList::addInfo(string data1,string data2, int data3)
{
BookNode* n = new BookNode;
n->Book_Title = data1;
n->Author_Name = data2;
n->Year_of_Publishing = data3;
n->next = NULL;
curr = head;
However, for this its giving the error
LinkedList::addInfo(<error-type>, <error-type>, int)" (declared at line 27 of
What am i doing wrong?
In order to use string as parameter type in the header you need to do two things:
The header must have #include <string>
Replace string with std::string, or add using namespace std (not recommended)
#include <string>
class LinkedList {
...
void addInfo(std::string, std::string, int);
};
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.