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 get an input variable in main function. and I want to use this variable in the external function2 that it already is called as a external function again by function1(like the code below). but this variable is undefined at function2. please help me. thanks for all answers. this is an overview of my code:
int main ()
{
int boogh;
cin >> boogh;
function1 (x,y);
}
function1(int x,int y)
{.
.
function2(w,z);
.
.
}
function2(int w, int z)
{
if (boogh>5)
{.
do some thing
.
.
}
}
the function1 and function2 are recursive
The variable is scoped within main, so you can't access it there. You need to get it (or a copy of it) into the scope of function2. One way is to pass it as a function parameter - to both functions, since it has to go through function1 to reach function2:
void function1(int x, int y, int boogh) {
//...
function2(w, z, boogh);
//...
}
void function2(int w, int z, int boogh) {
if (boogh > 5) { // the value is available here
//...
}
}
int main() {
int boogh;
cin >> boogh;
function1(x,y,boogh);
}
Or you could encapsulate the variable and the function(s) that use it in a class:
struct thingy {
int boogh;
void function1(int x,int y) {
//...
function2(w, z);
//...
}
void function2(int w,int z) {
if (boogh > 5) { // class member accessible here
//...
}
}
};
int main() {
thingy t;
cin >> t.boogh;
t.function1(x,y);
}
Or you could use a global variable, but that's nearly always a bad idea.
You must pass the required variable to the functions either by value or by reference:
Passing by value for your case:
// Example program
#include <iostream>
#include <string>
using namespace std;
void function1(int x);
void function2(int x);
int main ()
{
int boogh;
cin>>boogh;
function1 (boogh);
}
void function1(int x)
{
function2(x);
}
void function2(int y)
{
int boogh=y;
if (boogh>5)
{
//do something here
}
}
Related
I just noticed something when creating functions. In the code:
#include <iostream>
using namespace std;
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
it will work because the function being called is on top of the caller, but if I put the function add() below the calling function in main() it won't work.
#include <iostream>
using namespace std;
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
and the compiler will tell me that the identifier add() cannot be found.
so why do we declare functions anyway? like this:
#include <iostream>
using namespace std;
int add(int a, int b = 20);
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b)
{
int r;
r = a + b;
return (r);
}
A definition is implicitly a declaration. And a declaration must come ahead of the use.
All functions need to be declared before they are used.
You can do that by either (1) writing a declaration, or (2) writing a definition.
Relying solely on (2) can be tempting but then you are bound to order your program in a particular way, and is occasionally impossible. For example, the following will not compile unless the comment is removed.
//void bar(int);
void foo(int n)
{
if (!n){
bar(n);
}
}
void bar(int n)
if (n){
foo(n);
}
}
int main()
{
foo(1);
}
No.
If the function definition appears before the function call, then prototype is not mandatory. Otherwise function prototype is necessary to let compiler know how to respond to a function when it is called.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
if the function definition appears after the function call then prototype is mandatory. because it tells the compiler to how to respond the function when it is called.
check the following example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int, int); // function prototype
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
and if the function definition is made before the function call then it is not mandatory to declare function prototype.
consider example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
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
I am newbie to C++. Learning constructors. Please refer to two codes mentioned below, and provide reason, why Code 2 is not working. Thanks.
Code 1:
#include <iostream>
using namespace std;
class Box
{
int x;
public:
Box::Box(int a=0)
{
x = a;
}
void print();
};
void Box::print()
{
cout << "x=" << x << endl;
}
int main()
{
Box x(100);
x.print();
}
Code 2:
#include <iostream>
using namespace std;
class Box
{
int x;
public:
Box(int a=0);
void print();
};
Box::Box(int a=0)
{
x = a;
}
void Box::print()
{
cout << "x=" << x << endl;
}
int main()
{
Box x(100);
x.print();
}
Why the code 1 is working but Code 2 is NOT working?
For some odd reasons you are not allowed to repeat the default value for a parameter:
class Box
{
int x;
public:
Box(int a=0);
//------------^ given here
void print();
};
Box::Box(int a=0)
//------------^^ must not be repeated (even if same value)
{
x = a;
}
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);
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.