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 am kind a beginner to c++ and i dont really understand much about pointers.
There is an error in the code below. Soldier is a class in my program. The error states that 'targetsoldier was not declared in this scope'.
void level::battle(soldier *soldier, int targetx, int targety)
{
int x, y;
int enemyarmy;
soldier->getposition(x, y);
soldier *targetsoldier = getsoldier(targetx, targety);//THE ERROR OCCURS IN
THIS LINE
if(targetsoldier == nullptr){
return;
}
enemyarmy = targetsoldier->getarmy();
if(enemyarmy == soldier->getarmy()){
return;
}
int result = targetsoldier->takedamage(soldier->attack());
if(result ==1){
for(int h=0; h < _armies[enemyarmy].size(); h++){
if(_armies[enemyarmy][h] == targetsoldier) {
_armies[enemyarmy][h] = _armies[enemyarmy].back();
_armies[enemyarmy].pop_back();
delete *targetsoldier;
settile(targetx, targety, ' ', nullptr);
break;
}
}
}
}
The problem is that your function has a parameter named soldier; the name of that parameter then hides the name of the class soldier when it's in scope (i.e. inside the function). There are two possible solutions:
The sane one: rename the parameter (or the class)
The alternative: use class soldier instead of just solider to refer to the type when the parameter is in scope.
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 3 years ago.
Improve this question
I am just working on a program to find topological order if possible But when I am executing the below program I am getting error
as no matching function call,I am not so good at oops concept , I mentioned in findOrder function
a comment where I am getting the error
I was solving this problem course schedule
class Solution {
public:
vector<int> findOrder(int n, vector<vector<int>>& pa) {
vector<int> g[n];
for(auto p : pa){
int u = p[1],v = p[0];
g[u].push_back(v);
}
vector<bool>visited(n,false);
vector<bool>instack(n,false);
vector<int>order;
for(int i=0;i<n;i++){
if(!visited[i]){
if(!dfs(i,visited,instack,g,order))return {};//getting error here
}
}
reverse(order.begin(),order.end());
return order;
}
bool dfs(int x,vector<int> &visited,vector<int> &instack,vector<int> g[],vector<int>order){
instack[x]=true;
visited[x]=true;
for(int i=0;i<g[x].size();i++){
if(instack[g[x][i]]==true)return false;
else if(!visited[g[x][i]] && !dfs(g[x][i],visited,instack,g))return false;
}
instack[x]=false;
order.push_back(x);
return true;
}
};
In the protoype of your function dfs you have defined the parameters instack and visited as vector<int>, but you are providing variables of type vector<bool> in the calling function.
The easiest way is probably to change:
bool dfs(int x,vector<int> &visited,vector<int> &instack,vector<int> g[],vector<int>order)
into
bool dfs(int x,vector<bool> &visited,vector<bool> &instack,vector<int> g[],vector<int>order)
This is happening because vector is a templated class and the implicit conversion of vector<int> to vector<bool> isn't defined.
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
In my program I have structure:
struct point {
float x;
float y;
};
Edit: I need to create
struct Path{
Point array[];
}
Initialize it with function init_path(Path *p, int size).
My question is, how to define the function?
Thanks in advance.
Your Path may be like:
struct Path {
point* points;
};
void init_path(Path *path, int size) {
path->points = new point[size]();
}
but why your professor wants a function instead of proper constructor/destructor remains a mystery. Here you still need to call delete[] on points somewhere. With following structure you don't need any init function and object will properly delete its resources.
struct Path {
Path(unsigned size) : points{ new point[size] } {}
~Path() { delete[] points; }
point* points;
};
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 am beginner in C++ in Eclipse environment. Could you explain whats wrong with this code.
#include "Shapes.h"
Shapes::Shapes(float l, float w)
{
length = l;
width = w;
float calculateArea()
{
float area = length * width;
return calculateArea;
}
}
Things wrong with your code:
You're not using the area variable in any way.
One of your } is in the wrong place.
Your calculateArea is probably a member function of Shapes and should as such be prefixed with Shapes::.
You aren't using the member initializer list.
Your indentation is inconsistent.
You forgot to add the class scope in the implementation of calculatateArea.
Your cpp code must look like
Shapes::Shapes(float l, float w)
{
length = l;
width = w;
}
float Shapes::calculateArea()
{
float area = length * width;
return area;
}
This is how your code should be:
#include "Shapes.h"
Shapes::Shape(float l, float w) : length(l), width(w) { }
float Shape::calculateArea()
{
return this->length * this->width;
}
I changed your constructor to use the constructor syntax. It is faster than the assignment you did before.
You should read this http://www.cplusplus.com/doc/tutorial/classes/
It even has the same exact example as you are doing.
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
I have been curious about why c++ does not allow declaring a static function argument as shown below:
int test(static int a )
{
return a;
}
int main()
{
test(5);
return 0;
}
output console shows:
error: storage class specifiers invalid in parameter declarations
error: storage class specified for parameter 'a'
Update #1:
I can achieve my requirements like follows:
int test(int a )
{
static int count = 0;// <-- I want to eliminate this line due to some project constraints.
count += a;
return count;
}
I cannot use passing arguments by reference if you suggest, I have already tried considering that option.
If there is any other way to accomplish above behavior, you're welcome.
Thanks.
To declare a function static you would do it as such
static int test(int a )
{
return a;
}
You are trying to pass in a "static int a" into a function but there's no reason to do that. You would instead declare
static int a;
somewhere in the class and then simply pass in a to the static method created above as so
test(a);
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 9 years ago.
Improve this question
let's say i have a class of type human and i want to create a method function called jim.punch(billy); i created jim and i created billy. how do i refer to jim when i'm writing the function? let's say whatever returns is based on their weight. so if billy is bigger, something will happen and something else will happen if jim is bigger. i just don't know how to use jim in the function
#include <iostream>
using namespace std;
class dog
{
public:
int age;
int weight;
int height;
int punch(int);
};
int jim::punch(x)
{
if (jim > x) <------------------
{
return //something
}
else
{
return something
}
int main()
{
human jim;
jim.age = 30";
jim.weight = 175;
jim.height = 6;
human billy; //etc
jim.punch(billy);
return 0;
}
You should really follow a good book (or at least a good online tutorial); your question shows confusion about very basic C++ concepts.
Nevertheless, here's an answer to your particular situation (omitting loads of details and important-but-not-for-this-particular-case concepts). human is a class. Classes can have data members and member functions. Many instances (or obejcts) of a class type can exist; each of these has its own values of data members.
Each member function can access the members (data and functions) of the instance on which it was invoked. It can do so explicitly (using the keyword this which represents a pointer to the instance), or implicitly (just naming the data member).
So here's how you might express your situation in code:
class human
{
//place data members such as age, height, weight here
public:
int punch(const human &target);
};
int human::punch(const human &target)
{
std::cout << "PUNCH!";
if (weight > target.weight) //equivalent to this->weight > target.weight
{
return /* something which represents this object winning */
}
else
{
return /* something which represents target object winning */
}
//Note: you should handle == case as well
}
int main()
{
human jim, billy;
jim.punch(billy);
}