Multiple headers, how to use function on Object [closed] - c++

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 5 years ago.
Improve this question
How do I access a variable or typedef in a header from a method? It seems that the typedef isn't global even though I included the header file, why?
I have the following situation:
Snake.h
#ifndef SNAKE_H
#define SNAKE_H
#include <utility>
class Snake {
public:
Snake(int difficulty, int posX, int posY) : difficulty(difficulty) {
position.first = posX;
position.second = posY;
}
inline std::pair<int,int> const getPosition() {
return position;
}
private:
typedef std::pair<int, int> Point;
Point position;
};
#endif // !Snake.h
Movement.cpp
#include "Movement.h"
#include "Snake.h"
Snake moveDown() {
Point dummy = SnakeObject.getPosition();
return .....;
}
Now obviously this doesn't compile since there is stuff missing, but the compiler fails to recognize the Point type in the Movement.cpp file.
Also, do I need a Snake pointer in the Movement.h so I can use the snake object to call getPosition?
I'm sorry for the vague description, also your help is much appreciated.

Point is declared as a private class member, as such it is not accessible to non-class members.
Either make it a public class member, use the underlying std::pair type instead (like the declared return type of the method actually specifies), or assign the return value to an auto.

Related

return std::map with a getter method [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
As the title indicates I'm trying to grab on to a std::map container and return it.
I get the following error: invalid use of template-name 'std::map' without an argument list
Now I pretty sure the reason has to do with templates, but simply haven't found an example that describes my specific situation.
My program i rather simple since I'm a newbie on parantal leave. It consists of these files:
main.cpp
Movie_Archive.hpp
Movie_Archive.cpp
Movie.hpp
Movie.cpp
Helper.hpp
Helper.cpp
I don't think anyone wants me to paste all the code so I've pasted the parts that I belive to be vital to my question. Code below:
Movie_Archive.hpp
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
std::map getMovieArchive();
};
Movie_Archive.cpp
std::map MovieArchive::getMovieArchive() {
return movie_archive;
}
main.cpp
TheMovieArchive.controlArchiveStatus(TheMovieArchive.getMovieArchive(), TheMovieArchive.getTitle());
// Checks if the movie title already has been entered
Thank soo much for taking a look. I hope someone can find a solution.
Kind regards//Alle
You probably mean
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
std::map<std::string, Movie> getMovieArchive();
};
i.e., you have to provide the template parameters in the return type of the getter as well.
BTW: you probably want to write
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
const std::map<std::string, Movie> & getMovieArchive();
};
i.e. returning a const reference instead of a copy of the internal data structure.

C++ access violation when writing to typdef struct [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 struct defined in a header file. Then I have a singleton class where I am trying to use the struct. When I call ResetVars() from another class I get an access violation when it hits the line that says test.numResponses = "TEST". I am assuming this has something to do with initialization but I haven't been able to solve it. I am new to c++ and I have no idea how to get around this. Thanks for any help.
struct.h
typedef struct POLL_DATA
{
std::string numResponses;
std::string type;
std::string question;
} POLL_DATA;
ControlPolls.h
class ControlPolls
{
private:
static bool instanceFlag;
static ControlExitPolls *controlSingle;
ControlExitPolls();
POLL_DATA test;
public:
static ControlExitPolls* getInstance();
void ResetVars();
};
ControlPolls.cpp
#include "ControlPolls.h"
bool ControlPolls::instanceFlag = false;
ControlPolls* ControlPolls::controlSingle = NULL;
//Private Constructor
ControlExitPolls::ControlExitPolls()
{
};
//Get instance
ControlPolls* ControlPolls::getInstance()
{
if(!instanceFlag)
{
controlSingle = &ControlPolls();
instanceFlag = true;
return controlSingle;
}
else
{
return controlSingle;
}
}
void ControlExitPolls::ResetVars()
{
test.numResponses = "TEST";
}
callingClass.cpp
ControlPolls *controlSingleton;
controlSingleton = ControlPolls::getInstance();
controlSingleton->getInstance()->ResetVars();
You've been struck by C++'s Most Vexing Parse, a compiler rule that says anything that could be a function declaration is a function declaration. The culprit is this line:
POLL_DATA testPoll();
testPoll is treated as the declaration of a function with return type POLL_DATA. Try removing the brackets, or writing simply POLL_DATA testPoll; which implicitly calls the compiler-generated default constructor.
Another larger problem is that testPoll is a member of A, but you've hidden it and declared a local variable in your constructor, A::A(). I suggest you remove the constructor altogether because the implicit constructor will suffice.
Some more notes on your code:
You've declared your class a but refer to it later as A.
You've written an implementation of a constructor for A without declaring it like a proper forward declaration.
Also, typedef struct is not needed in C++. It is sufficient and encouraged to write:
struct POLLDATA {
...
};

static object c++ "does not name a type" [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 7 years ago.
Improve this question
I have the following code:
// header.h
class Outer
{
class Mid
{
Mid();
~Mid ();
};
class Inner
{
private:
static Mid m_mid;
};
};
When I define the static private object in the cpp file, it gives me an error saying Mid does not name a type:
// header.cpp:
# include "header.h"
Mid Mid::m_mid;
{Begin definitions for Outer, Mid and Inner from here}
Any clue why the compiler would complain about this ? : Mid does not name a type
There is no class Mid; there is also no member of Mid named m_mid.
There is, however, a class Outer::Mid, and a class Outer::Inner that has a member named m_mid. Write those instead. :)
Outer::Mid Outer::Inner::m_mid;
You have to fully qualify the type name and the member name.
Outer::Mid Outer::Inner::m_mid;
Those errors are nothing about static private object, there come up because of the following.
; is missed at the end of each class definition
construct is private, but should be public
namespace is missed when declare m_mid;
after fixed, the following works
class Outer
{
public:
class Mid
{
public:
Mid(){}
~Mid (){}
};
class Inner
{
private:
static Mid m_mid;
};
};
int main(){
Outer::Mid m_mid;
return 0;
}

Using a typedef enum outside of a class that was declared inside the class [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 8 years ago.
Improve this question
In C++, I have something like this:
namespace myNS
{
class A
{
public:
typedef enum A_ENUMS { NONE, ALL } A_ENUMS;
};
}
I want to access that typedef, maybe with something like this:
namespace myNS
{
struct ASettings
{
myNS::A::A_ENUMS myASetting;
}
}
But I am getting an error that the A does not declare a type. Any help is appreciated.
UPDATE: Thank you for the suggestions and help. After suggestions, I updated the code. I was missing the fact that there is a namespace involved in this also. It works now.
Your typedef/enum declaration is private. Make it public and you will be able to access it from outside the class.
class A {
public:
typedef enum A_ENUMS { NONE, ALL } A_ENUMS;
};
In C++ classes the default access is private, so your type is not accessible from the struct. Use
class A
{
public:
typedef enum A_ENUMS { NONE, ALL } A_ENUMS;
};
However, C++ is not C, and you do not need to use typedefs in the way normally used in C. Try:
class A
{
public:
enum A_ENUMS { NONE, ALL };
};
It will work the same without unnecessary elements.
You make 2 mistakes:
typdef != typedef (mistyping)
You forget class visibity.
Your code after corrections:
class A
{
public:
typedef enum { NONE, ALL } A_ENUMS;
};
struct ASettings
{
A::A_ENUMS myASetting;
};
int
main()
{
}
If you have normal C++11 support then you can rewrite enumeration with class scope like this:
typedef enum class A
{
NONE, ALL
} A;
Cheers.
See Updated section above. The namespace needed to be included.

Making a private function public in C++ [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 8 years ago.
Improve this question
I am learning my first programming language C++ and I have issues with making a private function public. Can you guys help me to find the problem?
#include <iostream>
#include <string>
using namespace std;
class JadClass
{
public:
void setName(string x)
{
name = x;
}
string getName()
{
return name;
}
private:
string name;
};
int main()
{
JadClass jc;
jc.setName = "Jad Charara w\n";
cout << jc.getName();
system("pause");
return 0;
}
instead of
jc.setName = "Jad Charara w\n";
write
jc.setName("Jad Charara w\n");
First of all you have defined 2 functions in class JadClass with public access specifier,so please confirm access specifier of which function you want to change from private to public.
Second thing in main you are trying to call setName function.
jc.setName = "Jad Charara w\n";
The above function call should be in jc.setName("Jad Charara w\n"); format.