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 6 years ago.
Improve this question
I came across this code, and I was interested what the constructs marked below by comments //<-- This are.
If it has a name then I would like to know (to google it and get more info if possible).
#include <stdio.h>
typedef struct point {
float x,y;
void print(void);
} dot;
typedef struct rect {
dot pt1,pt2;
float area(void);
dot center(void);
void print(void);
} rectangle;
void dot::print(void){ //<-- This
printf("(%3.1f,%3.1f)", x, y);
}
void rectangle::print(void){ //<-- This
pt1.print(); printf(":"); pt2.print();
}
dot rectangle::center(void){ //<-- This
dot c; c.x=(pt1.x + pt2.x)/2;
c.y=(pt1.y + pt2.y)/2; return c;
}
float rectangle::area(void){ //<-- This
return((pt2.x-pt1.x)*(pt2.y-pt1.y));
}
They are implementations of the functions defined in the classes (structs) abouse. Usually though, you would do this in your cpp file, so you would have your h file with:
class Foo{
int method1();
int method2();
}
and then in your cpp file you would add the implementation using:
int Foo::method1(){
....
}
This code is a bit silly though, because the classes are defined in ye olde c way using the typedef struct syntax. This would make sense in some cases, because c code is also valid c++ so you could have code that compiled as both. However c++ is not always valid c and this code id definitely c++ because of the member functions, so there is no point in using the typedef struct syntax. It is probably old code that has been modified.
The lines you are pointing to refer to the declaration of a function. I will explain one of these lnes, because you can apply the same lgic to the rest of them.
Let's look at the line:
void dot::print(void){
The first word in this line, void, defines the type of data returned fromthe function. Since it is void, no value is returned from this function, which is evident fomthe fact that there is no return statement in the entire function.
void dot::print(void) {
printf("(%3.1f,%3.1f)", x, y); // this is the last line of the function. This function does not pass on any value or data
}
Next is dot::, which is an object of struct point. If you see after the closing } of the struct point, you wil see that dot is declared here.
For the object dot, there is a function declaration called print(). This function is defined here, but since we have to indicate that we have to indicate that print() is a member of dot, we add the dot:: before the print(void) in the declaration.
Lastly is the void in parenthesis. This simply means that the function has no input parameters from the function that has called it; in other words, it does not need any data from outside the function.
Just as a recommendation, your code is more c than c++. You would be better off tagging this question as c instead of c++.
Related
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 3 years ago.
Improve this question
Have homework and we just went over classes. Thinking about how I could implement classes into my homework but the instructions say use a structure. (It's reading binary files and editing information) After looking at some stuff we did in class I probably don't need classes for my situation. But now I'm curious, why would you want to use a structure inside a class? Why would you use a structure when you can just set the data members in the class instead? What does struct have to offer to warrant doing this?
The primary benefit of putting values into an inner struct as opposed to just declaring them as member variables of the class would be that you can then instantiate multiple instances of that struct very easily, and refer to the set of variables in each struct with a single pointer or reference.
As an example, here are two implementations of a toy array-of-3D-points class. The first one just uses separate member-variables, while the second one declares an inner struct to represent a Point object.
Note that in the first implementation, the RotatePoint() method takes four arguments, while in the seconds argument it takes just two. Being able to refer to a Point with a single struct Point & argument (rather than three separate float & arguments) is both more efficient at run-time and less error-prone for the programmer to call.
// implementation without an inner struct
class MyPointArray1
{
public:
[...]
void RotatePoints(float radians)
{
for (int i=0; i<POINTS_ARRAY_LENGTH; i++)
{
// Hazard here -- a tired programmer might specify arguments in the wrong order!
RotatePoint(x[i], y[i], z[i], radians);
}
}
private:
enum {POINTS_ARRAY_LENGTH = 100};
float x[POINTS_ARRAY_LENGTH];
float y[POINTS_ARRAY_LENGTH];
float z[POINTS_ARRAY_LENGTH];
void RotatePoint(float & x, float & y, float & z, float radians)
{
// [math to update the values of x, y, and z would go here]
}
};
// implementation with an inner struct
class MyPointArray2
{
public:
[...]
void RotatePoints(float radians)
{
for (int i=0; i<POINTS_ARRAY_LENGTH; i++)
{
// It's pretty much impossible to get this wrong without provoking a compile-time error :)
RotatePoint(points[i], radians);
}
}
private:
enum {POINTS_ARRAY_LENGTH = 100};
struct Point
{
float x;
float y;
float z;
};
struct Point points[POINTS_ARRAY_LENGTH];
void RotatePoint(struct Point & pt, float radians)
{
// [math to update the values in (pt) would go here]
}
};
From my understanding, you would implement a struct inside a class when you want to create objects within that class only. Outside of the class, you would not be able to create objects of that struct.
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 5 years ago.
Improve this question
I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++
thanks
I am sorry if it is a fool question
It creates an object named some by instantiating the class some.
Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.
class is blue because it is a keyword of the C++ language.
The first some is green because it is the name of a class.
The second some is black because it is a variable.
And function and ToVector are red because the are functions.
Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.
Here is a more complete and nicer version:
#include <vector>
class Some
{
public:
std::vector<int> ToVector()
{
return std::vector<int>(); //return an empty vector
}
};
int f(std::vector<int> v)
{
return 0;
}
int main(int, char**)
{
Some some; // Was "class some some"
return f(some.ToVector());
}
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 {
...
};
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 6 years ago.
Improve this question
I have a myVector class:
class myVector {
public:
void populateVector();
void showMenu(vector <myVector> const &vec_first);
private:
vector <myVector> &vec_first;
}
I haven't filled my vector yet but I want to essentially use the vector in the showMenu() function, however, a problem arises in my main when I attempt to call the showMenu() function.
int main() {
myVector obj;
obj.showMenu(vector <myVector> const &vec_first);
}
Codeblocks keeps saying:
main.cpp|33|error: expected primary-expression before 'const'
Your are confusing the function declaration with calling it. You need
int main() {
myVector obj;
vector<myVector> vec;
obj.showMenu(vec);
}
or something like that
I haven't filled my vector yet but I want to essentially use the vector in the showMenu() function, however, a problem arises in my main when I attempt to call the showMenu() function.
Don't pass the vector (or anything) in through showMenu; it already has access to the vector, which is a member of the same class.
If you did want to pass a function argument, repeating the argument's original declaration would not be the way to do it. Only its name should be specified. Here that would be:
obj.showMenu(obj.vec_first);
… if vec_first weren't private.
It looks like you need to go back to basics and read the initial chapters of your C++ book.
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 7 years ago.
Improve this question
I have some code which I could not compile it yet. The compiler says there should be a closing bracket, but I can't see a reason for this or place to put it. This is my code:
#include "Player.h"
Player(std::string val){
set_Name(val);
set_Alliance("NONE");
set_LastUpdate();
}
Player(std::string val, std::string ally){
set_Name(val);
set_Alliance(ally);
set_LastUpdate();
}
I have included in Player.h
This is the error:
error: expected ')' before 'val'
This is the prototype for constructor:
Player(std::string);
I am using GNU GCC compiler, under linux(ubuntu)
You are missing the class name from constructor outside the class definition. Try this:
Player::Player(std::string val){ // constructor outside class definition
set_Name(val);
set_Alliance("NONE");
set_LastUpdate();
}
Unverified speculation: With your current code, compiler sees Player(symbol1 symbol2) and takes that as creating object of class Player, and first thing it fails to understand is seeing two symbols as constructor argument, and gives a somewhat misleading error about that.
When you define methods, constructor, destructor etc. outside of the class, remember to tell the compiler that this belongs to the class using the class name following the scope operator :: and the name of the method, constructor, destructor etc with the matching parameters.
As a small example:
class Phone {
string number;
public:
string get_num();
void set_num(string const &num) { number = num; }
};
// Pay attention to this:
// we tell the compiler that get_num belongs to class Phone
string Phone::get_num()
{
return number;
}
int main()
{
Phone p;
p.set_num("123");
cout << p.get_num() << endl;
}