Why should I use a closing bracket in this? [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 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;
}

Related

Why do I get "functions that differ only by return type cant't be overloaded" error when nothing is really overloaded? [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 12 months ago.
Improve this question
I have following example of c++ code in visual studio 2022:
#include<iostream>
#include<string>
employee get_employee() {
employee out = { 1, "John"};
return out;
}
class employee {
public:
int id;
std::string name;
};
int main() {
std::cout << get_employee().name;
return 0;
}
But when I run it, I get compiler complaining about get_employee(), specifically that "functions that differ only by return type cant't be overloaded".
But why does it do so, if I dont have another get_employee() definition anywhere in my code?
I know that I can't create an instance of an class before I define the class itself, and moving get_employee() definition below employee class definition really solves the issue, but it doesn't explain why compiler says that "functions that differ only by return type cant't be overloaded" instead of saying that you "cant crate an istance of a class before defining the class itself", and I would like to know why.
The problem here is fairly simple. You're trying to use employee before you've defined what it means. Move your definition of employee before the definition of get_employee.
#include<iostream>
#include<string>
class employee {
public:
int id;
std::string name;
};
employee get_employee() {
employee out = { 1, "John"};
return out;
}
int main() {
std::cout << get_employee().name;
return 0;
}
As to why the compiler gives a poor error message, my guess is that somehow or other the undefined name for the return type is tricking it into accepting the call to get_employee as it would have in old C, where a function without a declaration is presumed to return type int.
Then when it encounters the actual definition of get_employee, it has one entry already saying get_employee returns int and takes no arguments, and now sees what it's thinking is an attempt at overloading that also takes no arguments, but returns a client instead.
And that convinces it that what it's seeing is an attempt at overloading that uses the same parameter list (i.e., no parameters) but a different return type.

What is this construct: "void dot::print(void){"? [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 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++.

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 {
...
};

C++ eclipse. Abstract class won't compile [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 7 years ago.
Improve this question
I got a problem regarding Eclipse when creating abstract classes. I'm not very used to dealing with header files and such, my code basically looks as follows: (not displaying everything, just the basic class to give an idea of how it looks)
Equipment.h
namespace Equipments {
class Equipment{
public:
virtual ~Equipment();
virtual std::string get_category() const = 0;
protected:
Equipment(std::string name);
private:
const std::string name_;
};
class Weapon : public Equipment {
public:
Weapon(std::string name, std::string something_else);
virtual ~Weapon();
std::string get_category() const override { return "Weapon"; };
private:
const std::string something_else_;
};
} //end of namespace
Now, I got a problem with both the .h and .cpp file
in the .h file, under the weapon constructor, I'm used to writing (since I don't use header files):
Weapon(std::string name, std::string something_else)
: Equipment{name}, something_else_{something_else}
{}
Since I can't do this really (to my knowledge), how do I send parameters to my parent class? (in this case, letting eclipse know that I want my name parameter sent to Equipment parent class)
Should I do it in the .cpp file and if so, how?
And now the second problem.
In the .cpp file I create my equipment class like this:
namespace Equipments {
Equipment::Equipment(std::string name) {
name_ = name;
}
Equipment::~Equipment() {
}
std::string Equipment::get_name()
{
return name_;
}
//etc
But I can't seem to create my Weapon class. If I try:
Equipment::Weapon(std::string name, std::string something_else)
I just get member decleration not found, and if I try:
Weapon::Weapon(std::string name, std::string something_else)
I get no matching function for call to 'Equipments::Equipment::Equipment()'
I'm just stuck with not knowing how eclipse want me to write my code, I know it's a bit of a noob problem but I haven't been using either header files or c++ in eclipse for quite a long time. I'm really close to just pick my laptop and program in ubuntu and gedit instead so I don't have to deal with the header classes, however, then I won't learn anything either.
But I can't seem to create my Weapon class. If I try:
Equipment::Weapon(std::string name, std::string something_else)
I just get member declaration not found,
Well, yes: your Weapon class is called Weapon, and it's constructor is called Weapon::Weapon. It inherits from Equipment, it isn't stored inside it.
and if I try:
Weapon::Weapon(std::string name, std::string something_else)
I get
no matching function for call to 'Equipments::Equipment::Equipment()'
That just means you left out the base class constructor (so it's trying to use the default constructor, which doesn't exist) - you don't show the whole code, but it ought to be
Weapon::Weapon(std::string name, std::string something_else)
: Equipment(name)
, something_else_(something_else)
{
}

What is this header error caused by? [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 8 years ago.
Improve this question
I'm using the header file "sales.item"
I'm writing a little program and it is telling me that the header file, not my program, has an error. Somehow that last line isn't right. The error is saying that the string isbn is private.
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2;
if (item1.isbn() == item2.isbn()) { // this checks if item1 and item2 are same book
In the Sales_item class you forgot to make the isbn method public, and left it at its default private visibility.
It should, in short, read something like this:
class Sales_item
{
public:
return_value isbn();
}
Without the public: line it will be private by default in C++ classes.
I'm going to go out on a limb and guess that your class is defined something like this:
class Sales_item
{
std::string isbn;
}
Classes and structs have public, private, and protected labels for their member data, and classes have their members labelled as private by default. You should change it to read:
class Sales_item
{
public:
std::string isbn;
}
EDIT:
When you add () (with or without parameters) to an identifier, you are telling the compiler to call it like a function. Take out the ()'s and your code should work.