Clarification on a circular dependancy [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
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.

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.

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

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.

Passing a string as parameter 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 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);
};

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?