What is this header error caused by? [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 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.

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.

Constructor with ostream and istream parameters [closed]

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 2 years ago.
Improve this question
I've got a question implementing a class constructor that has istream and ostream parameters. These values are to be used within the scope of the class. I am building a game that will ask questions and I want to use the istream parameter to collect the user input and the ostream to show things in the console.
class MyClass{
public:
MyClass();
MyClass(ostream& show, istream& userInput);
~MyClass();
void anotherFunction(string name, string lastName);
void executeProgram();
Can anyone explain a solution, and provide sample code, to make the scope of istream within the class accessible? How would I call this in the main class?
Edit:
Hi and thank you for trying even i dont have clear output on this one.
What i am really looking for is to use this constructor as user interface of my program. This is a text based game which will accept 3 chars as options. I wanted to use this constructor to gather input. I hope that makes sense.
I don't see any particular problems here (and your question hasn't mentioned any). For example
#include <iostream>
#include <fstream>
using namespace std;
class MyClass
{
public:
MyClass() : _in(cin), _out(cout) {}
MyClass(istream& in, ostream& out) : _in(in), _out(out) {}
private:
istream& _in;
ostream& _out;
};
int main()
{
ifstream in("in.txt");
ofstream out("out.txt");
MyClass mc(in, out);
...
}
The idiomatic C++ way is to not take the two stream parameters in constructor but define insertion and extraction operators for your class.
Goes like this:
class MyClass
{
public:
/* define various accessors here */
};
ostream& operator<<(ostream& os, const MyClass& instance) { /* write out your class's representation here. */ }
istream& operator>>(istream& is, MyClass& instance) { /* set state in instance reading it from is. */ }

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

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

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)
{
}