Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The function call below generates compiler error:
void myfun() {}
myfun(); // error:
// gcc -- error: expected constructor, destructor, or type conversion before ';' token
// clang -- error: C++ requires a type specifier for all declarations
int main()
{
// ...
}
Can somebody explain it? How is the function call confused with a declaration? Why does the error go away if the function call is moved inside main()?
You cannot execute function calls outside of functions unless you use them for variable initialization. So you should write ...
void myfun() {}
int main()
{
myfun();
}
or
int myfun() { return 1; }
int dummy = myfun();
int main() {
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
Is there any way to use a class member function from outside?
As far as I know for this it would be necessary to somehow inject a private component into it.
For example (this is just an example the notFooClass function does not compile):
class FooClass{
private:
int i;
public:
FooClass(int x){
this->i = x;
}
int f(int num){
return i+num;
}
};
int notFooClass(int num1, int num2){
return FooClass(num1)::f(num2); //
}
int main(){
FooClass x(10);
std::cout<<x.f(5)<<std::endl;
std::cout<<notFooClass(10, 5)<<std::endl;
return 0;
The output should be:
15
15
Is it even possible to do something similar?
public methods can be called from outside. Thats what they are for.
notFooClass creates an instance and calls a member function. Thats basically the same as you do in main. The difference is only that you are using an unnamed temporary and wrong syntax:
int notFooClass(int num1, int num2){
return FooClass(num1).f(num2);
}
or with a named object to illustrate the similarity to main:
int notFooClass(int num1, int num2){
FooClass x(num1);
return x.f(num2);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a problem with constructors and member functions. When I try to compile the program, it shows: error:
‘int Vehicle::range()’ cannot be overloaded.
What should I do in this case ?
#include <iostream>
using namespace std;
class Vehicle{
public:
int passengers;
int fuelcap;
int mpg;
int range();
Vehicle(int p,int f,int m);
int range();
};
Vehicle::Vehicle(int p,int f,int m){
passengers=p;
fuelcap=f;
mpg=m;
}
int Vehicle::range(){
return mpg*fuelcap;
}
I haven't shown the main function because the problem must be here.
I expect the output of 336 and 168.
You're declaring Vehicle::range twice inside the class body:
int range();
Vehicle(int p,int f,int m);
int range();
Remove the second one and it should work.
Here's a minimal example, replicating the error you're getting.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#include<iostream>
class FooA
{
private :
friend class FooB;
void Hello();
void Hello2();
private:
void Hello3();
int m_iData;
};
class FooB
{
void fun()
{
FooA objA;
objA.Hello(); // right
objA.Hello2(); // right
objA.Hello3(); // right
//ojbA.m_iData = 0; // compile error
}
};
usually if we access Data member Function or Member variable which is private by an object directly throws error.
but in this scenario how it is able to access Hello(),Hello2(),Hello3() Functions and why it's throwing error in accessing m_iData.
The b and the j in objA have been swapped to form the unknown identifier ojbA.
Change ojbA.m_iData = 0; to objA.m_iData = 0;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
How can I fix this issue? What does that error mean? Please explain as I am almost done with this script. I just have to clear that error
fraction fraction::print() const
{
int num=numerator;
int den=denominator;
int a = 0;
if(num>den)
{
for(int counter=2;counter<den;counter++)
{
while(num%counter==0 & den%counter==0)
{
num=(num/counter);
den=(den/counter);
}
}
}
else
{
for(int counter=2;counter<num;counter++)
{
while(num%counter==0 & den%counter==0)
{
num=(num/counter);
den=(den/counter);
}
}
}
cout<<num<<"/"<<den;
}
Your function must return an object of type "fraction".
If you only want to print some value in a function, just define the return type as void.
Your function does not return anything. Use a return statement (such as return 10 with the value being a fraction as you declared) or change your function to void (if it shouldn't return anything).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <iostream>
using namespace std;
void kghg();
void menu(float kgs);
float kghg(float kilogram, float kgs){
kilogram=menu(kgs);
float hektogram;
hektogram=(kilogram*10);
return hektogram;
}
int main()
{
menu();
kghg()
return 0;
}
void menu(float kgs){
cout<<"Input values : ";cin>>kgs;
}
void kghg(){
float hektogram, kilogram;
hektogram=kghg(kilogram);
cout<<"Result : "<<hektogram<<endl;
}
when I try to compile this code, then show notice " error : void value not ignored as it though to be" and "error : too few arguments 'void menu(float)'".
Please someone help me.
You call the function menu as
menu();
Though it is declared as
void menu(float kgs)
You said that the function would take a float argument, but did not pass it one.
As a side note, it looks like you are trying to cin a value into kgs. If you want this to act as you expect, you need to pass kgs by reference, otherwise you will input a value to a copy of kgs, then the original would be unchanged. You could change menu to
void menu(float& kgs)
Then call it as
float kg; // Declare a float variable
menu(kg); // Pass that float by reference to your function