error:function definition does not declare parameters in setter - c++

I wrote a simple c++ program and it gave me error in the setter, function definition does not declare parameters, although there was a setter above it and it worked correctly the error is in the function named setattack here is the code:
{
#include <iostream>
using namespace std;
class Warrior{
private:
string name;
int attack;
int defense;
public:
void setname(string m){
name=m;
}
string getname(){
return name;}
void setattack{int aw}{
attack=aw;
}
int getattack(){
return attack;}
void setdefense{int dw}{
defenset=dw;
}
int getdefense(){
return defense;}
void kill(const Monster &monster){
}
};
int main()
{
return 0;
}
}

In your set function you have used {} curly braces for arguments I think it should be (), and there is an extra } after main.

Related

How to access a private structure within a class in C++?

Good day everyone! I would just like to ask if how it is possible to access a private structure inside a class?
My code looks like this:
class VideoClass{
private:
struct vidstruct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
};
public:
VideoClass();
// ~VideoClass();
void insertVideo(vidstruct info);
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
What I wanted to is to access the vidstruct (name of the structure) to any parts of my program. Thankyou for your kind answers in advance :)
The fact that your member vidstruct is declared as private means that it is not accessible outside of the class. If it is meant to be used outside of the class You could declare it simply outside of it.
Here would be a snippet example (printing 'movie test'):
#include <iostream>
using namespace std;
typedef struct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
} vidstruct;
class VideoClass{
public:
VideoClass() {
};
// ~VideoClass();
void insertVideo(vidstruct info) {
cout << info.movietitle;
};
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
int main()
{
vidstruct x;
x.movietitle = "movie test";
VideoClass videoTest;
videoTest.insertVideo(x);
return 0;
}

do we have access to variables inside an INLINE function after its execution has ended,like what we don't have in normal user defined function?

calculation() function is not working when m making input() function outside the class...has it got something to do with inline function??
#include <iostream>
using namespace std;
class price
{
public:
int pen;
int rubber;
int scale;
void input()
{
cout<<"enter the variables\n";
cin>>pen>>rubber>>scale;
cout<<"\n"<<pen<<" "<<rubber<<" "<<scale;
}
};
void calculate(price p)
{
int rate[2],total;
rate[0]=p.pen*5;
rate[1]=p.rubber*3;
rate[2]=p.scale*4;
total=rate[0]+rate[1]+rate[2];
cout<<"\n"<<total;
}
int main()
{
price a,b,c;
a.input();
calculate(a);
return 0;
}
No we don't. inline has no effect at all on the semantics of a C++ function. It's only effect is on how that function is treated by the linker.

error: use of undeclared identifier 'std' c++

I am new to coding in C++ and I want to make a simple Pokemon game.
I created a class in a header file and I am defining the functions in a separate .cpp file.
I also have a main file where I will run my actual code.
So I am defining a std::string function in my functions file, and it says std is an undeclared identifier.
Here are each of my files:
Function Definition:
#include "fns.hpp"
int Pokemon::getHP() {
return hp;
}
int Pokemon::getAttack() {
return attack;
}
int Pokemon::getDefense() {
return defense;
}
int Pokemon::getSpecialAttack() {
return specialAttack;
}
int Pokemon::getSpecialDefense() {
return specialDefense;
}
int Pokemon::getSpeed() {
return speed;
}
std::string Pokemon::getAttack1() {
return attack1;
}
std::string Pokemon::getAttack2() {
return attack2;
}
std::string Pokemon::getAttack3() {
return attack3;
}
std::string Pokemon::getAttack4() {
return attack4;
}
Pokemon::Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4)
: hp(qhp),attack(qattack),defense(qdefense),specialAttack(qspecialAttack),specialDefense(qspecialDefense),speed(qspeed),attack1(qattack),attack2(qattack2),attack3(qattack3),attack4(qattack4) {}
Function Declaration:
class Pokemon {
int hp,attack,defense,specialAttack,specialDefense,speed;
std::string attack1,attack2,attack3,attack4;
public:
int getHP();
int getAttack();
int getDefense();
int getSpecialAttack();
int getSpecialDefense();
int getSpeed();
int getAttack1();
int getAttack2();
int getAttack3();
int getAttack4();
Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4);
};
Whenever I say std::string, it says it is an undeclared identifier.
Can someone please help me?
It is because you have not used the library for it.
use the below at the top of your header file
#include<string>

Calling correct virtual methods

Here is my code:
#include <iostream>
#include <string>
using namespace std;
class Sport{
protected:
string name;
double hours;
virtual double returnCalories()=0;
public:
Sport():name("Not defined"),hours(0.0){}
Sport(string n, double c):name(n),hours(c){}
virtual ~Sport(){}
void setName(string x){
name=x;
}
void setTime(double x){
hours=x;
}
};
class Running:public Sport{
public:
static const int CALORIES = 950;
Running(){}
~Running(){}
double returnCalories(){
return hours*CALORIES;
}
};
class Activity{
public:
Sport* one;
Activity(){}
Activity(string n,double time){
one->setName(n);
one->setTime(time);
}
~Activity(){}
};
class Diary{
private:
Activity list_activity[10];
int counter_activity;
public:
Diary():counter_activity(0){}
~Diary(){}
void addActivity(Activity x){
// add activities
}
double sumCalories(){
for(int i=0;i<10;i++){
if(list_activity[i].one->getName()=="Running"){
// I want to call returnCalories() of class Running
}
}
}
};
int main() {
Activity test("Running",3.2);
Diary test2;
test2.addActivity(test);
return 0;
}
Now I have a question:
How is it possible to call returnCalories() of class Running where I want to ? ( it's commented in the code )
Is this even possible, or should I change my logic somehow ?
It's crashing because you have not initialized Sport *one; and you're attempting to call methods on a null pointer. You need to first create a Running object within the Activity constructor using the "new" operator like so:
one = new Running(n, time);
Create an overloaded constructor in your "Running" class that takes the appropriate arguments as well, so that you can initialize your variable as shown above.

What does "void-value is not ignored" error mean and how to remove it?

I try to compile the following code:
#include <cppunit/extensions/HelperMacros.h>
#include "tested.h"
class TestTested : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestTested);
CPPUNIT_TEST(check_value);
CPPUNIT_TEST_SUITE_END();
public:
void check_value();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestTested);
void TestTested::check_value() {
tested t(3);
int expected_val = t.getValue(); // <----- Line 18.
CPPUNIT_ASSERT_EQUAL(7, expected_val);
}
As a result I get:
testing.cpp:18:32: Error: void-value is not ignored where it should be
EDDIT
To make the example complete I post the code of the tested.h and tested.cpp:
tested.h
#include <iostream>
using namespace std;
class tested {
private:
int x;
public:
tested(int int_x);
void getValue();
};
tested.cpp
#include <iostream>
using namespace std;
tested::tested(int x_inp) {
x = x_inp;
}
int tested::getValue() {
return x;
}
you declare void getValue(); in the class tested.. change to int getValue();.
A void function cannot return a value.
You are getting a value of int from the API getValue(), hence it should return an int.
Your class definition doesn't match the implementation:
In your header you've declared it in the following way (as an aside, you might want to look into some naming conventions).
class tested {
private:
int x;
public:
tested(int int_x);
void getValue();
};
You've declared getValue() as void, i.e no return. Doesn't make much sense for a getter to return nothing, does it?
However, in the .cpp file you've implemented getValue() like so:
int tested::getValue() {
return x;
}
You need to update the getValue() method signature in the header type so that its return type matches the implementation (int).