I have written a snippet that tries to use an object from a class as a function parameter, and I keep getting the error
In function 'int main()':
75:23: error: expected primary-expression before 'test'
75:35: error: expected primary-expression before 'Knight'
I am not sure how to fix this, as I am quite new to C++.
Some example code is down below:
// Example program
#include <iostream>
#include <string>
using namespace std;
//player class
class Player {
public:
//variable declaration
string name;
string classType;
int strength, perception, endurance, charisma, intelligence, agility, luck;
int id, cubes; // currency etc.
bool authority;
int inventory[20] = {};
int health = 100 + ((strength * endurance) * 2);
//sub stat functions
double getPick() {
return ((intelligence + luck) * (agility / 4)) * .01;
}
double getSneak() {
return (25 + (agility * 5)) * 0.01;
}
double getIntimidation() {
return (charisma * 10) * 0.01;
}
double getBarter() {
return getIntimidation();
}
double getScience() {
return ((intelligence * 5) / 3);
}
};
//enemys
class enemy {
public:
//var declaration
string name;
int HP;
double AC; //armor class ablity to resist hits
int DT; //dice used to attack
int eid; //id for enemys (enemy id)
int gear[2] = {}; //gear
//is the enemy alive?
int alive() {
if (HP <= 0) cout << "\nThe " << name << " is dead! ";
return false;
}
};
//fight an enemy (option 1)
int fightEnemy(Player player1, enemy enemy1) {
cout << "\n" << player1.name << " and a " << enemy1.name << "\n";
return 0;
}
int main() {
//test
Player test;
test.name = "test";
test.classType = "test";
test.strength = 3;
test.perception = 3;
test.endurance = 6;
test.charisma = 2;
test.intelligence = 6;
test.agility = 3;
test.luck = 5;
test.id = 1;
test.authority = true;
test.cubes = 500;
enemy Knight;
Knight.name = "Knight";
Knight.HP = 20;
Knight.AC = 0.2;
Knight.DT = 12;
Knight.eid = 3;
fightEnemy(Player test, enemy Knight);
return 0;
}
fightEnemy(Player test, enemy Knight);
Syntax is wrong here. You just pass the variables to the function, you are essentially declaring them again.
It should be
fightEnemy(test, Knight);
fightEnemy(Player test, enemy Knight);
to
fightEnemy(test, Knight);
The variables are already initialized.
Related
I'm studying for an exam and this is on my practice test. The question is "Which type of error does the following code fragment cause?"
Why is there an error?
struct C2D {
double x, y;
};
class Polygon {
int point;
C2D arr[];
public:
Polygon(int point_, C2D arr_[]) {
point = point_;
memcpy(arr, arr_, sizeof(C2D) * point);
};
void print() const {
for (int i = 0; i < point; i++) {
cout << arr[i].x << " " << arr[i].y << endl;
}
};
};
int main() {
C2D c2d[3];
c2d[0].x = 1;
c2d[0].y = 2;
c2d[1].x = 3;
c2d[1].y = 4;
c2d[2].x = 5;
c2d[2].y = 6;
Polygon p1(3, c2d);
p1.print();
return 0;
}
You didn't specify number of elements for the member
C2D arr[];
so there are no memory allocated for that.
You should use std::vector to allocate elements dynamically.
#include <iostream>
#include <vector>
#include <cstring>
using std::cout;
using std::endl;
struct C2D {
double x, y;
};
class Polygon {
int point;
std::vector<C2D> arr;
public:
Polygon(int point_, C2D arr_[]) : arr(point_) { // allocate point_ elements for arr
point = point_;
memcpy(arr.data(), arr_, sizeof(C2D) * point); // copy data using data()
};
void print() const {
for (int i = 0; i < point; i++) {
cout << arr[i].x << " " << arr[i].y << endl;
}
};
};
int main() {
C2D c2d[3];
c2d[0].x = 1;
c2d[0].y = 2;
c2d[1].x = 3;
c2d[1].y = 4;
c2d[2].x = 5;
c2d[2].y = 6;
Polygon p1(3, c2d);
p1.print();
return 0;
}
I'm new at programming. I wondered if you were to call a specific argument in the parameter for your constructor, which you had to use in your method, then how would you do it with pointers? I solved it by the use of 'this' because the local variable name is the same as the member variable's name, but I just wanted to see how it is possible by the use of normal pointers and not the this pointer.
Here is the code I worked on.
BMR.h
#include <string>
using namespace std;
class BMR
{
public:
BMR();
BMR(char, int, int, int);
int calculateBMR();
private:
int weight;
int height;
int age;
char gender;
};
BMR.cpp
#include "BMR.h"
#include <iostream>
using namespace std;
BMR::BMR()
{
}
BMR::BMR(char k, int h, int w, int a)
{
gender = k;
height = h;
weight = w;
age = a;
}
int BMR::calculateBMR()
{
int BMR;
if(this->gender == 'M')
{
BMR = 10 * weight + (6.25 * height - (5*age)) + 5;
cout << "Man: " << BMR << endl;
}
if(this->gender == 'W')
{
BMR = 10 * weight + (6.25 * height - (5*age)) - 161;
cout << "Woman: " << BMR << endl;
}
}
I'm having this issue with code evaluation. I want to use a function in one class, and use a function in another but I can't seem to get a prototype working for the function in the class.
class Pride
{
public:
std::string firstMinion = "Pride";
int prideAttackPlayer()
{
int damage = (rand() % 20 + 1) * attack;
return damage;
}
int damagePride()
{
health = health - play.attackEnemy();
}
int outputHealth()
{
return health;
}
private:
int health = 20;
int attack = 0;
int defence = 0;
int money = 0;
}pride;
class Player
{
public:
int attackEnemy()
{
int damage = (rand() % 20 + 1) * attack;
return damage;
}
void userAttack()
{
getline(std::cin, userInput);
if (userInput == "1")
{
std::cout << pride.damagePride();
}
}
private:
int health = 100;
int attack = 0;
int defence = 0;
std::string userInput = "";
}play;
I want to use play.attackEnemy in the Pride class but it won't work due to evaluation errors. I think the fix is to use a prototype but I've tried with int Player::attackEnemy() {}; and class Player but it doesn't do anything.
I am currently programming a sportsmanagement software.
You can add clubs and members into those clubs, however you can also add subclubs to clubs themselves.
Problem: If i make a new Society and add new members, the cost calculation works.
Club 1: Member 1, Member 2, Member 3 --> cost calculation is correct
if i now add a second club the cost calculation for that Club is correct as well.
Club 2: Member 5, Member 6. --> cost calculation is correct
However if i now add Club 2 as a branch of Club 1, the calculation is still the same one as it was without the branch of Club 2
Club 1: Member 1, Member 2, Member 3 , Club 2 -> calculation not correct.
I would really appreciate your help. Down below is my code.
Members.cpp
#include <iostream>
#include <string>
#include "Members.h"
static int memberid = 0;
Members::Members()
{
this->name = "default";
this->id = memberid++;
}
Members::Members(std::string name)
{
this -> name = name;
this->id = memberid++;
}
Members::~Members()
{
}
void Members::setname(std::string newname)
{
this -> name = newname;
}
std::string Members::getName()
{
return this->name;
}
int Members::getID()
{
return this->id;
}
double Members::getIncome()
{
return this->income;
}
void Members::setIncome(double income)
{
this->income = income;
}
double Members::getCosts()
{
return this->costs;
}
void Members::setCosts(double costs)
{
this->costs = costs;
}
double Members::getSurplus()
{
return this->income - this->costs;
}
std::string Members::toString()
{
std::string formated = " Member: " + getName() + "\n Income: " + std::to_string(getIncome()) + "\n Costs: " + std::to_string(getCosts()) + "\n Surplus: " + std::to_string(getSurplus());
return formated;
}
Society.cpp in Society.h it is class Society : public Members
#include "Society.h"
#include "Membertypes.h"
#include <sstream>
#include <vector>
#include <algorithm>
Society::Society(std::string name, int maximalmembers)
{
setname(name);
setMax(maximalmembers);
Members::setCosts(this->getCosts());
Members::setIncome(this->getIncome());
}
double Society::getIncome()
{
double income=0;
for (int i = 0, n = members.size(); i < n; i++)
{
income += members[i].getIncome();
}
return income;
}
double Society::getCosts()
{
double costs=0;
for (int i = 0, n = members.size(); i < n; i++)
{
costs += members[i].getCosts();
}
return costs;
}
double Society::getSurplus()
{
double surplus=0;
for (int i = 0, n = members.size(); i < n; i++)
{
surplus += members[i].getSurplus();
}
return surplus;
}
void Society::addMember(Members* newMember)
{
if ((int) members.size() < this -> maximalMembers)
{
this ->members.push_back(*newMember);
}
else
{
std::cout << "To many members" << std::endl;
}
}
Thanks for your help
To an extend this is a wild stab in the dark, but you have a std::vector<Members> members; inside the Society class.
You have designed Members as a polymorphic class - you have virtual functions, and the various constructors set different costs etc to each type.
HOWEVER, you just slice these when you put them in the vector.
Your add method:
void Society::addMember(Members* newMember)
{
if ((int) members.size() < this -> maximalMembers)
{
this ->members.push_back(*newMember);
}
else
{
std::cout << "To many members" << std::endl;
}
}
takes a pointer - which is a potential memory leak - and then treats this as a Member.
Consider using a std::vector<std::shared_ptr<Members>> instead.
I'm a noobie at C++ and I was making a game for practice on Visual Studios and I just couldn't figure out how to update stats when exp was added. I tried changing the player level by adding exp but when I add 55 exp the player remained at level 1 still.
Main:
#include <iostream>
#include <windows.h>
#include "Game.h"
using namespace std;
void FalseLoad();
int main() {
//Cool load intro
FalseLoad();
cout << "\n \n";
Game::Game();
system("PAUSE");
return 0;
}
void FalseLoad() {
int i = 0;
int start;
cout << "***Freelancer*** \n \n";
system("PAUSE");
while (i <= 100){
cout << "Loading game... " << i << "% \n";
i++;
Sleep(110 - i);
if (i == 100) {
start = 0;
}
}
}
Game.cpp:
#include <iostream>
#include "Game.h"
using namespace std;
Game::Game() {
Player Player;
Player.Init();
cout << Player.exp << " " << Player.level;
Player.exp += 55;
cout << " " << Player.exp << " " << Player.level << " ";
}
Game.h:
#pragma once
#include "Player.h"
class Game {
public:
Game();
};
Player.cpp:
#include "Player.h"
Player::Player() {
}
void Player::Init() {
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
}
Player.h:
#pragma once
class Player
{
public:
Player();
void Init();
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
};
If you add 55 to exp, only exp will be changed.
You can write getters and setters and declare the member variables private:
Player.h
#pragma once
class Player
{
public:
Player();
void Init();
void addExp(const int additionalExp);
int getExp();
//... TODO add similar get/set methods for the other members...
private:
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
};
and add the method definitions:
#include "Player.h"
Player::Player() {
}
void Player::Init() {
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
}
void Player::addExp(const int additionalExp) {
if ( additionalExp < 0 ) return; // think about error handling or use
// unsigned for exp
exp += additionalExp;
level = exp / 50; // or something else, as you like.
}
int Player::getExp(){ return exp; }
// ... TODO add definitions for the other get/set methods...
And use the addExp() method in your main.cpp.
One benefit of having the member variables private is that you get more control in how they get manipulated. E.g. if you add exp, you can set level accordingly simultaneously.