This question already has answers here:
What are access specifiers? Should I inherit with private, protected or public?
(2 answers)
Closed 9 years ago.
How do you guys find the value of s.x from this code
I am beginner of c++ and dont know how to solve it
Please help thanks
// StarterLab.c : C Program to convert to C++
//
//#include "stdafx.h" // required for Visual Studio
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//#include "MemTracker.h"
#pragma warning (disable:4996)
using namespace std;
struct variable
{
friend void showCalculation(variable a);
private:
int x;
int y;
int sum;
public:
void Calculate(int x,int y);
};
void showCalculation(variable a)
{
printf("%d",a.sum);
};
void variable:: Calculate (int x,int y)
{
sum = x + y;
};
int main ()
{
variable s;
s.Calculate(7, 6);
showCalculation(s);
printf("%d",s.x);
}
How do you guys find the value of s.x from this code
I am beginner of c++ and dont know how to solve it
Please help thanks
The variable x is private, so you cannot access it directly. You could add a member function to get it:
int variable::GetX() {
return x;
}
printf("%d", s.GetX());
You can not access s.x because x is a private member. You have two options.
Create a getter:
int variable::X() { return x; }
or make it public:
public:
int x;
int y;
int sum;
Note that using getters/setters is the appropriate way of doing this.
Related
This question already has an answer here:
error: no matching function for call to ‘Point::Point()
(1 answer)
Closed 7 months ago.
I developed two modules with separate implementation and interfaces.
These are the ones:
This is file Move.h:
#pragma once
#include "utils.h"
class Move {
private:
int x;
int y;
public:
Move(int x_inp, int y_inp);
char getX();
int getY();
};
And this is Move.cpp:
#include "Move.h"
Move::Move(int x_inp, int y_inp) {
int size = 10
this->x = x_inp;
this->y = y_inp;
};
int Move::getX() {
return this->x;
};
int Move::getY() {
return this->y;
}
This is the file Node.h:
#pragma once
#include "Move.h"
#include <vector>
class Node {
private:
Move move;
Node* parent;
std::vector <Node*> children;
public:
Node(Move inp_move, Node* parent_inp);
double value(const float EXPLORE_CONST);
void add_children(std::vector<Node*>);
};
#include "Node.cpp"
Node::Node(Move inp_move, Node* parent_inp) { // this is where compilation error rises.
this->move = inp_move;
this->parent = parent_inp;
}
void Node::add_children(std::vector<Node*> list_of_children) {
for (Node* item : list_of_children) {
this->children.push_back(item);
}
}
It always gives me the error that no default constructor exists for class "Move". I am really stuck and try to figure out the solution but didn't find the answer.
Can you guys help me please.
Thanks
you should try literally adding a default constructor to your "Move" class.
public:
Move() {}
//default constructor
Move(int x_inp, int y_inp);
char getX();
int getY();
};
´´´´
You need use member initializer lists:
Node::Node(Move inp_move, Node* parent_inp)
: move{inp_move}
, parent{parent_inp}
{
}
Or provide default constructor for Move as suggested in comment. IMO use of member initializer lists is better choice.
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 5 years ago.
Improve this question
i saw a few posts with same problem but i didn't not manage to understand how do i make the temporary object to constant
Map.H
#ifndef _Map_
#define _Map_
class Map
{
private:
int Board[7][7];
public:
Map();
Map(int mapNum);
~Map();
void print() const;
};
#endif
Map.Cpp basicly just creates a 7*7 array with 0 or 1 in all places
Robots.H
#ifndef _Robot_
#define _Robot_
#include "Map.h"
class Robot
{
private:
int _RobotID;
int _mapNum;
int _X;
int _Y;
public:
Robot();
~Robot();
Robot (int mapNum, int Line, int Column);
void setRobotID(int newid);
void print() const;
};
#endif
s
Robot.cpp
#include "Robot.h"
#include "Map.h"
#include <iostream>
#include "Game.h"
using namespace std;
Robot::Robot()
{
}
Robot::Robot(int mapNum, int line, int column) {
_mapNum = mapNum;
_X = line;
_Y = column;
_RobotID=0;
}
now creating a map in my main works and so does printing it.
same goes for robot.
what i want to do is connect the robot and the map inside my "game.cpp\game.h" so that each robot that i add will check in the map (double array with 0's or 1's )if it has a 1 it wont add it to map. and if it has a 0 it will.
(addRobot function is suppose to do that)
Game.H
#ifndef _Game_
#define _Game_
#include <vector>
#include <iostream>
#include "Map.h"
#include "Robot.h"
class Game
{
private:
static int _RobotsNum;
Map map1;
Map map2;
public:
void AddRobot(int mapnum, int x, int y);
Map getMap(int mapnum);
Game();
~Game();
};
#endif
Game cpp
#include "Game.h"
#include <algorithm>
#include <vector>
using namespace std;
int Game::_RobotsNum = 0;
vector <Robot> RobotVec;
Game::Game()
: map1(1),
map2(2)
{
}
Game::~Game()
{
}
void Game::AddRobot(int mapnum, int x, int y) {
my main
int main() {
Game game;
// Game* pgame = new Game();
game.AddRobot(1, 3, 4);
game.AddRobot(1, 4, 4);
game.AddRobot(1, 5, 4);
hope you guys can help me. thanks
This constructor has three local variables with the same names as other variables:
Game::Game()
{
vector <Robot> RobotVec; // Not your global variable
Map map1(1); // Not your member variable
Map map2(2); // Not your member variable either
}
In order to initialise members, you use the initialiser list:
Game::Game()
: map1(1),
map2(2)
{
}
In addRobot, this creates a robot and points X at it:
Robot* X = new Robot;
This also creates a robot, so now you have two:
Robot newRobot(mapnum, x, y);
And this memory leak points X away from its original robot and instead points it at newRobot, which will be destroyed immediately afterwards:
X = &newRobot;
Note that addRobot does not at any point add either robot to anything – it creates two and ignores them both.
You should make the vector a member (avoid global variables unless repeating other people's mistakes is a particular passion of yours):
class Game
{
private:
int robotsNum;
vector<Robot> robotVec;
Map map1;
Map map2;
// ...
};
Game::Game()
: robotsNum(0),
map1(1),
map2(2)
{
}
And add your new robot to the vector:
void Game::AddRobot(int mapnum, int x, int y) {
// ...
Robot newRobot(mapnum, x, y);
robotsNum++;
newRobot.setRobotID(robotsNum);
robotVec.push_back(newRobot);
}
Robot newRobot(mapnum, x, y);
This creates an object of type Robot named newRobot. At the end of the block where it was created it will be destroyed.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I've just started looking at C++ and I am having some problems I can't wrap my head around.
I am trying to do something as simple as creating a class with a function, call that function and read the returned value.
However I am stuck at what I guess is a minor issue.
I know that it is the call from main to Die -> randomNumber that causes this error but I dont know why. I even tried to return the number 12 from the function without any luck.
Error:
I have 3 files (main.cpp, Die.h and Die.cpp)
main.cpp
#include <string>
#include "Die.h"
using namespace std;
int main() {
Die * test = new Die();
cout << std::to_string(test->randomNumber(4)) << endl;
return 0;
}
Die.h
#pragma once
#include <iostream>
#include <ctime>
#include <cstdlib>
class Die
{
public:
Die();
~Die();
void init();
void rollDie(int&, int&);
void rollDie(int*, int*);
int randomNumber(int);
};
Die.cpp
#include "Die.h"
Die::Die()
{
}
Die::~Die()
{
}
void Die::init() {
}
int randomNumber(int max) {
srand(static_cast<unsigned int>(time(0)));
return (rand() % max + 1);
}
void rollDie(int& die1, int& die2) {
}
void rollDie(int* die1, int* die2) {
}
You're missing the class specifier on the declaration of randomNumber and rollDie.
int Die::randomNumber(int max)
Without those you're just declaring a global function named randomNumber, but the linker is attempting to choose Die::randomNumber which it cannot find.
In Die.cpp, you need to prefix member functions with the class name:
int Die::randomNumber(int max) { ... }
void Die::rollDie(int& die1, int& die2) { ... }
void Die::rollDie(int* die1, int* die2) { ... }
Without that, the compiler thinks you're just defining global functions. It has no reason to suspect they should be members to a class if you don't tell it.
This question already has answers here:
Undefined reference to a static member of the class
(2 answers)
Closed 7 years ago.
I have a code like this. When run, it's return fault: undefined reference to A::x. How can I fix this?
#include <iostream>
using namespace std;
class A
{
private:
static int x;
public:
A(){
}
A(int t) {
x = t;
}
static void f() {
cout<< A::x;
}
int f2() {
return x;
}
};
int main() {
A::f();
A a;
a.f2();
}
You have only declared your static variable, not defined it.
Define it outside of the class using:
int A::x = 0;
You need to add
int A::x = 0; //Or any other value
somewhere outside your class declaration.
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 7 years ago.
Improve this question
Is there a way to allocate a variable within the scope of a parameter list? By using new we can do the following :
Class A{ /*... snip ...*/ };
void myFunc(A* a){ }
int main(...){
myFunc(new A());
return 0;
}
This will create a new A. What if the signature of myFunc was
void myFunc(A a);
instead. Is there a syntax to create local instance inside the myFunc() parameter list? I'm looking for something like
myFunc(A());
or
myFunc(A a());
Another use would be for something like :
A a(123);
if(a == A(123)){ }
The net effect is to save one line, but it also creates a scope within the parameters list which makes me wonder if it is allowed at all.
If you just want to create a variable to pass to the function you can use a aggregate initialization / list initialization
#include <iostream>
#include <cmath>
using namespace std;
class A{ /*... snip ...*/ };
void myFunc(A a){ }
int main(){
myFunc(A{});
return 0;
}
Live Example
You can also use this with classes that have constructors that take multiple parameters
#include <iostream>
#include <cmath>
using namespace std;
class A
{
private:
int foo;
int bar;
double foobar;
public:
A(int a, int b, double c) : foo(a), bar(b), foobar(c) {}
};
void myFunc(A a){ }
int main(){
myFunc(A{1,2,3.0});
return 0;
}
Live Example
C++ supports this with the myFunc(A()); syntax you posed in your question.
#include <stdio.h>
char lazybuff[500];
class Point
{
public:
Point (double x, double y) : m_x(x), m_y(y) { }
char * ToString (void) { sprintf (lazybuff, "%f, %f", m_x, m_y); return lazybuff; }
private:
double m_x, m_y;
};
void print_point (Point print_me)
{
printf ("%s\n", print_me.ToString());
}
int main (void)
{
print_point (Point(5, 3));
return 0;
}