Random consts on initialization lists [closed] - c++

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.

Related

Adding namespace functions in header files [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 4 years ago.
Improve this question
I'm trying to use header files to transfer functions in a namespace on a separate file to another file, and it's returning the error
"Add" is not a member of "simple".
I simply want to know if it's possible to use forward function declarations rather than declaring the function every time in the header, which supposedly would keep copying the code for every .cpp that includes it.
Here's the basic outline for 1.cpp, 2.cpp and 3.h.
1.cpp
#include "stdafx.h"
// Check this header file for function declarations.
#include "3.h"
int main()
{
//Example of a single namespace.
std::cout << simple::add(3, 4);
return 0;
}
2.cpp
#include "stdafx.h"
namespace simple {
int simple::add(int x, int y) {
return x + y;
}
}
3.h
#ifndef NAMESPACES
#define NAMESPACES
namespace simple {
int simple::add(int, int);
}
#endif
Much appreciation to anyone who answers, and apologies if this has been asked before.
Figured out the issue, for any future viewers of this thread.
You don't use the prepending "simple::" on header declarations, or in the function itself, as I did.
Corrected code:
3.h
#ifndef NAMESPACES
#define NAMESPACES
//Header guards are important!
namespace simple {
int add(int, int);
}
#endif
2.cpp
#include "stdafx.h"
#include "3.h"
int simple::add(int x, int y) {
return x + y;
}
No changes are required to 1.cpp.

How to sort text lines in C++ using The Alphanum Algorithm? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to use The Alphanum Algorithm to sort some text lines contained in *.txt files.
I've seen this site:
http://www.davekoelle.com/alphanum.html
And there's a *.cpp file that contains this algorithm.
But I don't know how to use it.
So let's say I have code like that:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
file.open("My_textfile.txt");
[ALPHANUM SORTS My_textfile.txt --> don't know how to do it :( ]
file.close();
return 0;
}
Is there any simple way to perform sorting using this algorithm?
Best regards,
Mike
The .cpp is dependent on MFC, and is useless, since it does not come with the corresponding class definition. You should use alphanum.hpp (it's a header-only library) if you want to use the algorithm in your project.
To sort, use the stl sort function.
Example:
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include "alphanum.hpp"
int main()
{
std::vector<std::string> v;
// fill array ...
std::sort(v.begin(), v.end(), doj::alphanum_less<std::string>());
// v is sorted !
return 0;
}

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.

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?