This question already has answers here:
C++ what to code if i put a class after main() function
(5 answers)
Closed 7 years ago.
If the function is defined after main() function program will work...
void printdata(int i);
int main()
{
printdata(20);
return 0;
}
void printdata(int i)
{
std::cout << "i = " << i << std::endl;
}
If we declare class before main function and defined after main function, why it will through error?
#include <iostream>
class C;
int main()
{
C c(20);
c.printdata();
return 0;
}
class C
{
int i;
public:
C(int a) : i(a) {};
void printdata()
{
std::cout << "C:i = " << i << std::endl;
}
};
Error log after compiling the code:
class_after_main.cpp: In function ‘int main()’:
class_after_main.cpp:7:6: error: variable ‘C c’ has initializer but incomplete type
C c(20);
If you declare a class without defining it, you can only use reference or pointer to that class. The compiler needs to know the size of the class to define the size needed to store its objects.
Related
This question already has answers here:
How to print class object using operator<<
(3 answers)
Closed 3 months ago.
#include <iostream>
using namespace std;
class PrintName{
public:
void studentName(){
cout<<"Name : "<<studentName<<endl;
}
};
class MathClass{
public:
void multiplicationFunc(int x, int y){
cout<<"Result : "<<(x*y)<<endl;
}
};
int main()
{
cout<<endl;
PrintName PN;
PN.studentName("Mark", "Santo");
MathClass MC;
MC.multiplicationFunc(10,5);
}
I am new to C++ and am learning about classes and objects. From what I gathered classes are ways to group functions and objects is the ability to access them? I am having trouble getting this code to work, I receive an error on line 15 for 'error: no match for 'operator<<'. I am trying to fix my class in order for the main function to work. The output should simply be
'Name : Mark Santo'
'Result : 50'
Thank you for your help everyone!
It seems like there is no error on line 15. Instead, you did not define the variable studentName in your void studentName() function in line 8. Also, you used 2 arguments for studentName() in line 23, where the original function is not taking any. This is the corrected code:
#include <iostream>
using namespace std;
class PrintName{
public:
void studentName(string x, string y){
cout<<"Name: " << x << " " << y << endl;
}
};
class MathClass{
public:
void multiplicationFunc(int x, int y){
cout<<"Result: " << x*y << endl;
}
};
int main()
{
cout << endl;
PrintName PN;
PN.studentName("Mark", "Santo");
MathClass MC;
MC.multiplicationFunc(10,5);
}
I'm beginning to learn C++. In the IDE codeblocks, this compiles:
#include <iostream>
using namespace std;
struct A {};
struct B {
A a;
}
void hi() {
cout << "hi" << endl;
}
int main() {
hi();
return 0;
}
But this doesn't:
struct B {
A a;
}
struct A {};
int main() {
hi();
return 0;
}
void hi() {
cout << "hi" << endl;
}
It gives me the errors:
error: 'A' does not name a type
error: 'hi' was not declared in this scope
Should class/function order matter in C++? I thought it doesn't. Please clarify the issue.
Yes, you must at least declare the class/function before you use/call it, even if the actual definition does not come until afterwards.
That is why you often declare the classes/functions in header files, then #include them at the top of your cpp file. Then you can use the classes/functions in any order, since they have already been effectively declared.
Note in your case you could have done this. (working example)
void hi(); // This function is now declared
struct A; // This type is now declared
struct B {
A* a; // we can now have a pointer to it
};
int main() {
hi();
return 0;
}
void hi() { // Even though the definition is afterwards
cout << "hi" << endl;
}
struct A {}; // now A has a definition
This question already has an answer here:
Why "Foo f(Bar());" can be a declaration of a function that takes type Bar and returns type Foo? [duplicate]
(1 answer)
Closed 4 years ago.
I have a C++ code that seems to be confusing a class contructor like A::A(B b) with a constructor that receives a function pointer, like A::A(B (*)()). Let me explain:
The following code compiles:
#include <iostream>
#include <cstring>
#include <vector>
struct Item {
Item() {
std::cout << "ITEM::Normal constructor\n";
}
};
struct Container {
Container(Item i) {
std::cout << "CONTAINER::Normal constructor\n";
}
void doSomething() {
std::cout << "Do something\n";
}
};
int main() {
Container c3(Item());
return 0;
}
But if I add a call to B::doSomething(), like the following code, I obtain a compiler error that I don't understand:
#include <iostream>
#include <cstring>
#include <vector>
struct Item {
Item() {
std::cout << "ITEM::Normal constructor\n";
}
};
struct Container {
Container(Item i) {
std::cout << "CONTAINER::Normal constructor\n";
}
void doSomething() {
std::cout << "Do something\n";
}
};
int main() {
Container c3(Item());
c3.doSomething();
return 0;
}
The compiling error is:
main.cpp: In function ‘int main()’:
main.cpp:23:6: error: request for member ‘doSomething’ in ‘c3’, which is of non-class type ‘Container(Item (*)())’
c3.doSomething();
It's a veiled most vexing parse issue:
Container c3(Item());
declares a function prototype, and your helpful compiler issues the appropriate diagnostic.
Container c3{Item()};
is the fix.
I have tried multiple google searches and help guides, but I'm out of ideas on this one. I have a function pointer that I am using as an argument for another function. Both functions are within the same class. However, I keep getting type conversion errors. I'm sure this is just a syntax problem, but I can't understand what the correct syntax is. Here is a simplified version of my code:
Header File
#ifndef T_H
#define T_H
#include <iostream>
#include <complex>
namespace test
{
class T
{
public:
T();
double Sum(std::complex<double> (*arg1)(void), int from, int to);
int i;
std::complex<double> func();
void run();
};
}
#endif // T_H
Source File
#include "t.h"
using namespace test;
using namespace std;
//-----------------------------------------------------------------------
T::T()
{
}
//-----------------------------------------------------------------------
double T::Sum(complex<double>(*arg1)(void), int from, int to)
{
complex<double> out(0,0);
for (i = from; i <= to; i++)
{
out += arg1();
cout << "i = " << i << ", out = " << out.real() << endl;
}
return out.real();
}
//-----------------------------------------------------------------------
std::complex<double> T::func(){
complex<double> out(i,0);
return out;
}
//-----------------------------------------------------------------------
void T::run()
{
Sum(&test::T::func, 0, 10);
}
Whenever I try to compile, I get the following error:
no matching function for call to 'test::T::Sum(std::complex<double> (test::T::*)(),int,int)'
note: no known conversion for argument 1 from 'std::complex<double> (test::T::*)()' to 'std::complex<double>(*)()'
Any advice appreciated. Or at least a link to a thorough site on how to use function pointers. I am using Qt Creator 2.6.2, compiling with GCC.
Your Sum function expects pointer to a function. And then you try to call it with a pointer to a member function. Learn about pointers to members.
The code itself is a bit messy, I'll only correct the grammer to make it work.
firstly, you shall change the function prototype from
double Sum(std::complex<double> (*arg1)(void), int from, int to);
to
double Sum(std::complex<double> (T::*arg1)(void), int from, int to);
Meaning that it is a pointer to class T's member.
Then, when calling the function, you cant just arg1(),
for (i = from; i <= to; i++)
{
out += arg1();
cout << "i = " << i << ", out = " << out.real() << endl;
}
you have to use (this->*arg1)();
for (i = from; i <= to; i++)
{
out += (this->*arg1)();
cout << "i = " << i << ", out = " << out.real() << endl;
}
How to pass functions as arguments in C++? In general, use a template, unless you have very compelling reasons not do it.
template<typename Func>
void f(Func func) {
func(); // call
}
On the call side, you can now throw in a certain amount of objects (not just pointers to functions):
Functors;
struct MyFunc {
void operator()() const {
// do stuff
}
};
// use:
f(MyFunc());
Plain functions:
void foo() {}
// use
f(&foo) {}
Member functions:
struct X {
void foo() {}
};
// call foo on x
#include <functional>
X x;
func(std::bind(&X::foo, x));
Lambdas:
func([](){});
If you really want a compiled function and not a template, use std::function:
void ff(std::function<void(void)> func) {
func();
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to call a function using pointer-to-member-function
Analyzer.h
class Analyzer
{
public :
void viku();
void Bibek();
void vivek();
void (Analyzer::*point)();
Analyzer(){
}
~Analyzer(){
}
};
Analyzer.cpp
using namespace std
#include"Analyzer.h"
void Analyzer::viku(){
cout<<"Hello viku";
}
void Analyzer::vivek(){
point =&Analyzer::viku;
Bibek();
}
void Analyzer::Bibek(){
point();//Errror
cout<<"Bibek";
}
During compilation it shows the following error:
error C2064: term does not evaluate to a function taking 0 arguments.
Can anyone please tell me how to avoid this?
Pointers to member functions are different than normal function pointer. You need an instance to call them:
#include <iostream>
class A
{
public:
int foo()
{
std::cout << "A::foo here, you can have 42" << std::endl;
return 42;
}
};
int main ()
{
int (A::* point)() = &A::foo;
A a;
(a.*point)();
}
In your case, you'd need to do something like the following:
(this->*point)()