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 check and recheck the following code, I don't see what the compiler is talking about. I am a novice with c++, so please be detailed!
the code is:
//myheader.h:
#ifndef MYHEADER_H
#define MYHEADER_H
class myHeader
{
public:
//mutators
void setWidth(int);
void setLength(int);
//accessors
int getWidth();
int getLength();
myHeader();
virtual ~myHeader();
private:
int width,
length;
};
#endif // MYHEADER_H
myheader cpp looks like this:
#include "myHeader.h"
myHeader::myHeader()
{
//ctor
/* void setWidth(int w);
void setLength(int l);
int getWidth();
int getLength();
*/
}
myHeader::~myHeader()
{
//dtor
}
//setWidth will assign a value to the private member width
void myHeader::setWidth(int w)
{
width = w;
}
//setLength will assign a value to the private member Length
void myHeader::setLength(int l)
{
length = l;
}
//getWidth will return the value for the width member
int myHeader::getWidth()
{
return width;
}
//getLength will return the value for the length member
int myHeader::getLength()
{
return length;
}
the main is not yet finished, and looks like this:
#include "myHeader.h"
#include <iostream>
using namespace std;
int main()
{
myHeader impleHeader;
int locLength = 0;
int locWidth = 0;
cout<<"width / length";
cin>>locLength>>locWidth;
myHeader.setLength(locLength);
myHeader.setWidth(locWidth);
return 0;
}
I don't see any problem, but a trained eye can definitely spot it. please tell me what am I doing wrong
myHeader is the name of a type, not an object.
You wanted to invoke setLength() and setWidth() on the object impleHeader:
impleHeader.setLength(locLength);
impleHeader.setWidth(locWidth);
Related
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 4 years ago.
Improve this question
I've added a C++ class to help with simulations I'm running. It was working fine, until I added another constructor with different inititation parameters, after which the compiler complains about an 'incomplete type' error with the original constructor that was working before. CLion also complains it can't find a matching constructor, even though its the same one I've been using until now.
PS. I'm creating 2 separate instances of the class (1 in main, 1 in another class also initialised in main)...not sure if this is maybe an issue?
Here's the code:
SimEnv.hpp
#ifndef SIMENV
#define SIMENV
#include "ClassContainingSomeParams.hpp"
class SimEnv
{
private:
int p1, p2;
public:
SimEnv(ClassContainingParams); // This is the bad constructor
SimEnv(int, int);
};
#endif
SimEnv.cpp
#include "SimEnv.hpp"
/* This is the bad constructor */
SimEnv::SimEnv(ClassContainingSomeParams p) :
p1(p.getP1()), p2(p.getP2())
{}
SimEnv(SimEnv(int p1, int p2) : p1(p1), p2(p2)
{}
ClassContainingSomeParams.hpp
#ifndef CLASSCONTAININGSOMEPARAMS
#define CLASSCONTAININGSOMEPARAMS
#include "SimEnv.hpp"
ClassContainingSomeParams
{
public:
ClassContainingSomeParams();
void runSim();
int getP1();
int getP2();
private:
int p1, p2;
};
#endif
ClassContainingSomeParams.cpp
#include "ClassContainingSomeParams.hpp"
ClassContainingSomeParams::ClassContainingSomeParams() : p1(0), p2(0)
{}
void ClassContainingSomeParams::runSim()
{
SimEnv env(p1, p2);
// Do some stuff
}
main.cpp
#include "ClassContainingSomeParams.hpp"
#include "SimEnv.hpp"
int main()
{
ClassContainingSomeParams p;
SimEnv env(p);
// Do some stuff
return 0;
}
The exact errors I'm getting are
SimEnv.hpp:10:33: error: field 'ClassContainingParams' has incomplete type 'SimEnv'
ClassContainingSomeParams.hpp:5:1: error: 'ClassContainingSomeParams' does not name a type
Do I need to place the class declaration and initialisation into the header file? If so, why?
There is a circular inclusion between ClassContainingParams.hpp and SimEnv.hpp.
change your ClassContainingParams to ClassContainingParams* or const ClassContainingParams& and move the inclusion of ClassContainingParams.hpp in SimEnv.cpp.
#ifndef SIMENV
#define SIMENV
class ClassContainingSomeParams;
namespace Namespace
{
class SimEnv
{
private:
size_t p1, p2;
size_t* pTracker;
void init();
public:
SimEnv(const ClassContainingParams&);
SimEnv(size_t, size_t);
size_t func1();
size_t func2();
};
}
#endif
SimEnv.cpp
#include <SimEnv.hpp>
#include <ClassContainingSomeParams.hpp>
namespace Namespace
{
SimEnv::SimEnv(const ClassContainingSomeParams& p) :
p1(p.getP1()), p2(p.getP2())
{
init();
}
SimEnv(SimEnv(size_t p1, size_t p2) : p1(p1), p2(p2)
{
init();
}
void SimEnv::init()
{
std::cout << "I'm initialised" << '\n';
}
}
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.
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
Why can't I put the definition of class constructor with parameters-initialize list outside the class declaration?
typedef unsigned int UINT;
class num_sequence {
public:
typedef vector<UINT>::iterator iterator;
//I cannot put this following part in the cpp file
num_sequence(vector<UINT> & ele,int len=0,int beg=0):_relems(ele),_length(len),_beg_pos(beg)
{
//an abstract class cannot be instanlized
cout<<"build a num_sequence object";
}
virtual ~num_sequence();
num_sequence.h
#include <vector>
typedef unsigned int UINT;
class num_sequence
{
public:
typedef std::vector<UINT>::iterator iterator;
num_sequence(std::vector<UINT> & ele, int len = 0, int beg = 0);
virtual ~num_sequence();
private:
std::vector<UINT> &_relems;
int _length;
int _beg_pos;
};
num_sequence.cpp
#include "num_sequence.h"
#include <iostream>
num_sequence::num_sequence(std::vector<UINT> & ele, int len, int beg)
: _relems(ele), _length(len), _beg_pos(beg)
{
std::cout << "build a num_sequence object";
}
num_sequence::~num_sequence()
{
}
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;
}
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.