Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

How can wrap some code with a method?

for example I have this code:
C++
if(x=y)
{
  agent.gotox();
}


and I want to put this in a method. It's not in main and its in other .cpp file.

Thanks
Posted
Updated 22-Apr-11 18:07pm
v2

Please kindly consider to stop asking any questions here at CodeProject until you learn the very basics of programming using some elementary book and do most simple exercises on all topic of, say, C++. It should give you enough confidence; and you also will be able to collect some more sensible questions.

When you get this experience, welcome back. This would only make all kind of help from CodeProject members much more effective. Right now, it's too much of time loss.

Thank you for your understanding.

—SA
 
Share this answer
 
v2
Comments
arashmobileboy 23-Apr-11 1:37am    
ok,thanks for your advice,but to become a c++ programmer with good experience i need to ask my questions,if you are sad of my questions please dont see the questions that the writer is me,thnanks
Sergey Alexandrovich Kryukov 23-Apr-11 21:32pm    
Thank you for your advice, but I think I already know what to see or pay attention for. The purpose of my comment is really to help you; and I disagree with you. You do not have to ask your questions, especially here, because it won't be effective. You need to read some book and learn what you can learn yourself first. With this initial background, your questions will be appreciated and the answers will really help you. Right now, this is not the case. I'm only trying to point out more effective way and save your time and time of others.
--SA
CPallini 23-Apr-11 10:40am    
Yep, definitely more practical, 5++!
Sergey Alexandrovich Kryukov 23-Apr-11 21:32pm    
Than you very much.
--SA
Do it in this way:
void my_function(int y,int& x)
{
  if(x=y)
  {
    agent.gotox();
  }
}

but remember:
if(x=y) is like x=y; if(0!=x) //...
Regards.
 
Share this answer
 
Comments
arashmobileboy 22-Apr-11 17:05pm    
x and y and agent are some variables that defined before this method,now how should it define?thanks
mbue 22-Apr-11 18:32pm    
Why do you downgrade answers? Refine your question to get better answers. Nobody can understand what you want from that fuzzy code snippet. You have to tell more from your code or what you want to do.
Regards.
Sergey Alexandrovich Kryukov 22-Apr-11 21:23pm    
All correct, my 5, but... I think OP is simply not ready yet.
Please see my answer -- I think it makes some more sense, practically...
--SA
arashmobileboy 23-Apr-11 1:28am    
ok,all of you are sad because of my vote,i will give 5 after i got my specific answer,i m not a programmer yet,but when i do,i wont be like you this much proud
Method of what class? And how is the agent object related to such class?
If the agent is a data member of the class then you may use something similar to:
C++
//MyClass header file
class MyClass
{
//...
Agent agent;
public:
  void checkAndGo(int x, int y);
//...

C++
// MyClass source file
void MyClass::checkAndGo(int x, int y)
{
  if (x==y)
    agent.goto(x);
}

on the other hand, if agent isn't member of the class then you've to change the method's signature, for instance:
C++
void checkAndGo(int x, int y, Agent & agent);
 
Share this answer
 
Comments
arashmobileboy 22-Apr-11 17:05pm    
x and y and agent are some variables that defined before this method,now how should it define?thanks
Albert Holguin 22-Apr-11 19:58pm    
He told you, read carefully
Sergey Alexandrovich Kryukov 22-Apr-11 21:23pm    
All correct (up-voted with my 5, hope it would compensate the bad vote...), but... I think OP is simply not ready yet.
Please see my answer -- I think it makes some more sense, practically...
--SA
C++
public void SendToX(Agent agent, int x, int y)
{
    if (x == y)
    {
        agent.gotox();
    }
}
 
Share this answer
 
Comments
CPallini 22-Apr-11 15:46pm    
That's C# code, John.
#realJSOP 22-Apr-11 19:29pm    
He's a programmer - he should be able to adjust. Right?
Sergey Alexandrovich Kryukov 22-Apr-11 21:16pm    
No, he is not a programmer; isn't that obvious? (Not that it's your fault :-)
--SA
CPallini 23-Apr-11 10:43am    
I second SA reply, however have my 5 to balance a little the idiot votes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900