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?
Related
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 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.
I'm current building an application in which I have a log function that is accessible in most of my classes which was declared as below:
FileHandler.h
#ifndef FILEHANDLER_H
#define FILEHANDLER_H
#pragma once
#include <SDL.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cctype>
//Include to allow logging
#include "log.h"
class fileHandler
{
public:
fileHandler();
virtual ~fileHandler();
void WriteToFile(const std::string& filename, std::string textToWrite);
std::vector<std::string> ReadFromFile(const std::string& filename);
std::string& TrimString(std::string& stringToTrim);
protected:
private:
class log logHandler;
std::vector<std::string> blockOfText;
std::string currentLine;
};
#endif // FILEHANDLER_H
Log.h
#ifndef LOG_H
#define LOG_H
#pragma once
#include <SDL.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <time.h>
class log
{
public:
log();
virtual ~log();
void static WriteToConsole(std::string textToWrite);
void WriteToLogFile(std::string textToWrite);
protected:
private:
};
#endif // LOG_H
This worked fine for a long time and then I wanted to include another function elsewhere in my application that was only compatible with C++11 so I told the compiler to compile to these standards. I was then receiving an error on "log logHandler" saying log is not a declared name.
I was able to resolve the problem by changing the line to
class log logHandler;
I was wondering if anybody could tell me what has changed between C++03 and C++11 that required me to do this?
EDIT: Included all relevant code to make question more complete.
You don't show your real code (missing ; at the end of the class declaration, no #endif), but chances are that your problem is somehow related to std::log, which has received a new overload in C++11, in combination with a using namespace std somewhere in your code.
Note that the new overload is probably irrelevant to the problem at hand; the real reason may very well be a change somewhere in your compiler's standard-library implementation causing an internal #include <cmath>. This means that even in C++03, your code was only working by sheer coincidence, and a conforming C++03 compiler was always allowed to reject it.
Here is an example program which may reproduce your problem:
#include <cmath>
using namespace std;
struct log
{
};
int main()
{
// log l; // does not compile
struct log l; // compiles
}
Nothing has changed about how the code you posted is treated.
What I suspect is, that you somewhere have an
#include <cmath>
And below that, somewhere else
using namespace std;
This causes your compiler to not be able to unambiguously resolve the name log, since there is std::log (a function) and your class log.
By explicitly stating class log, you tell the compiler that you are referring to the class.
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.