This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Closed 8 years ago.
I am using Code::Blocks 10.05 with GCC on Windows 7. I was experimenting with C++ constructors and I compiled and executed the following program.
#include<iostream>
using namespace std;
class base {
public:
base() {
cout<<"\n Constructor Invoked\n";
}
};
int main() {
base ob;
return 0;
}
The output was as expected and shown below.
Constructor Invoked
But while typing the program I accidentally compiled the following program. To my surprise it compiled without any error or warning.
#include<iostream>
using namespace std;
class base {
public:
base() {
cout<<"\n Constructor Invoked\n";
}
};
int main() {
base ob();
return 0;
}
But the program didn't give any output, just a blank screen. But no error or warning. Since it hasn't called the constructor I assume no object was created. But why no error or warning? Am I missing something very obvious?
When I added the line cout<<sizeof(ob); I got the following error message.
error: ISO C++ forbids applying 'sizeof' to an expression of function type
So what is ob? Is it considered as a function or an object?
Please somebody explain the line of code base ob(); and what actually happens in the memory when that line of code is executed?
Thank you.
You have declared a function with
base ob();
It will do nothing.
See here
Related
This question already has answers here:
Why does stdafx.h work the way it does?
(4 answers)
Closed 5 years ago.
I'm facing a really stupid and infuriating problem, I was following a video on how to make a roguelike, I decided to stop after knowing how to print a level to the screen and actually start coding, then I got multiple errors on seamingly perfect code, I blammed the constructors so I decided to make a new project on visual studio to test the most stupid case, one main fuction, one clase with one constructor that does nothing, and I leaved all the work of making the class and the constructor to visual studio, I still got the goddammned issue, here's the error on that simplified version of the code, I would show the rogue like code but I would have to translate to english al the errors myself, and most of them have nothing to do with the actual error, because mostly they're complaining about missing ; in places where either there shouldn't be a ; or there alredy is one.
here's the code
main.cpp:
#include "stdafx.h"
int main()
{
return 0;
}
test.h
#include "stdafx.h"
class test
{
public:
test();
};
test.cpp
#include "test.h"
#include "stdafx.h"
test::test(){
}
yet with that simple auto-generated code, visual studio still complaints and gives me this error messages
c4430 missing type specifier - int assumed. Note: C++ does not support default-int
c2653 'test' : is not a class or namespace name
'test' : function should return a value; 'void' return type assumed
what should I do?
You have to define return type explicitly and return a value of that type if your function returns not void. Otherwise you will get that error.
class A
{
int int_func();
void void_func();
};
int A::int_func()
{
return 0;
}
void A::void_func()
{
// No return;
}
This question already has answers here:
Injected class name compiler discrepancy
(3 answers)
Closed 6 years ago.
namespace fooo {
class Fooo {
public:
int a;
};
}
namespace fooo {
class Test {
public:
Test(Fooo::Fooo *i) {
i->a = 1;
}
};
}
This code compiles fine with clang (any version) but fails with gcc.
Can anyone explain why?
EDIT:
Yes, I know the issue here is kinda obvious but why does clang accept it? The person who told me this said that this is a bug in the standard and that there is a Defect Report. Can anyone point to the actual DR?
The error message from gcc tells you exactly what the problem is:
t.cpp:11:16: error: ‘fooo::Fooo::Fooo’ names the constructor, not the type
Test(const Fooo::Fooo *i) {
^
it is suprising that clang doesn't give an error.
This question already has answers here:
Why does typeid.name() return weird characters using GCC and how to make it print unmangled names?
(7 answers)
Closed 7 years ago.
Program:
#include<iostream>
#include<typeinfo>
using namespace std;
class Base
{
public:virtual void func(){}
};
class Derived1 : public Base {};
int main()
{
Derived1 d;
cout<<typeid(d).name()<<endl;
return 0;
}
Output:
8Derived1
What is this number 8?
Compiler: g++-4.8.4-2ubuntu1~14.04
It is implementation defined, and has no inherent meaning. Check std::typeinfo::name() reference :
Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given, in particular, the returned string can be identical for several types and change between invocations of the same program.
This question already has answers here:
get function member address
(2 answers)
Closed 8 years ago.
I am trying to use function pointer in a c++ class but got an error.
#include <cstdio>
#include <iostream>
using namespace std;
class abc{
public:
void hello(){
printf("Hello world\n");
}
abc(){
void (*hw)(void);
hw = &hello;
hw();
}
}
int main()
{
abc ab;
return 0;
}
Error
error: cannot convert ‘void (abc::*)()’ to ‘void (*)()’ in assignment
But the following code works for me which is in code base. Can anyone please help me to find out the difference?
void hello(){
printf("Hello world\n");
}
int main()
{
void (*hw)(void);
hw = &hello;
hw();
return 0;
}
Function pointers are (unfortunately) completely different from method pointers, as the error is trying to indicate. This is because the object that the method works on needs to be passed in somehow, which is what makes methods fundamentally different from functions in the first place (and obviously affects how the call is done through the pointer). Method pointers are not even always the same size (and can be significantly larger than a function pointer) when multiple/virtual inheritance comes into play.
You need to declare a method pointer, and call it on behalf of an object of the right type using one of the esoteric .* or ->* operators:
class abc {
public:
void hello(){
printf("Hello world\n");
}
abc() {
void (abc::*hw)();
hw = &abc::hello;
(this->*hw)(); // or (*this.*hw)()
}
}
This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Closed 9 years ago.
I am going through Nicola Josuttis's OOP in C++ book and experimenting with his code using Code Blocks IDE. I am having difficulty understanding the compiler error message. I created a simple class interface (frac1.hpp), a class (frac1.cpp), and a test with main() - (ftest.cpp). The class accepts two integers which is printed out as a fraction. The class constructors set a default of 0 if called w/o any arguments, an integer value if called with 1 argument, or a fraction if called with 2 arguments. If one or two arguments are passed there is no compile error. But if no arguments are passed I expected the constructor to be initialized to 0, instead I get a compiler error about the print statement being of "non-class type". It is as though the object wasn't created. Any help or explanation of what I am doing wrong is greatly appreciated.
thank you kindly for your consideration.
Class description:
//frac1.cpp
#include "frac1.hpp"
#include <iostream>
#include <cstdlib>
//default constructor
Fraction::Fraction() : numer(0), denom(1) //initialize fraction to 0
{
//no further statements
}
Fraction::Fraction(int n) : numer(n), denom(1) //whole integer initialization
{
//no further statements
}
Fraction::Fraction(int n, int d) : numer(n), denom(d)
{
if (d==0) {
std::cerr << "error: denominator is 0" <<std::endl;
std::exit(EXIT_FAILURE);
}
}
void Fraction::print()
{
std::cout<<numer<<'/'<<denom<<std::endl;
}
Interface Description:
//frac1.hpp
#ifndef FRAC1_HPP_INCLUDED
#define FRAC1_HPP_INCLUDED
#include <istream>
#include <cstdlib>
namespace CPPDemo {
// Fraction Class
class Fraction {
private:
int numer, denom;
public:
Fraction();
Fraction(int);
Fraction(int,int);
void print();
};
}
#endif // FRAC1_HPP_INCLUDED
test file description:
//ftest.cpp
#include "frac1.hpp"
#include <iostream>
#include <cstdlib>
int main()
{
CPPDemo::Fraction y();
y.print(); //flagged as compiler error**
}
Message from Compiler:
C:\Users\User\Desktop\CPPDemo\FractionClassTest\ftest.cpp:9: error: request for member 'print' in 'y', which is of non-class type 'CPPDemo::Fraction()'
Change the test file to:
int main()
{
CPPDemo::Fraction y;
y.print(); //flagged as compiler error**
}
Without the () the compiler does not see y.prints as a function call. My knowledge of C++ syntax rules is not good enough to give a better explanation. sorry.
change
CPPDemo::Fraction y();
**y.print;
to
CPPDemo::Fraction y;
y.print();
Because the first declares a function, it does not declare the object you wanted.
And the print function needs brackets (I don't know what the ** were for)
You forgot the function braces ().
Try
y.print()
Oh damn, that got me again. You also need to fix the instanciation
CPPDemo::Fraction y;