I use Turbo C++ and am experiencing an unexpected error in my code, please help..
I am trying to pass an array of objects to a member function.
An error : Undefined structure test , pops on the line where i define my print function
#include<iostream.h>
#include<conio.h>
class test
{
int t;
public:
void print(test T[])
{
cout<<"This Test\n";
}
};
void main()
{
clrscr();
test T1,T2[5];
T1.print(T2);
getch();
}
I have to use the outdated version of the Turbo C++ compiler at school so the syntax of the code might be different than the new compilers.
Define your function as void print(test *T).
Turbo C++ is broken in the regard of parameters of type test[] being equivalent to test*.
Related
I have a class that has a declared method but not defined/used anywhere. I expected this piece of code to generate linking error but it did not. Looks like compiler is smart enough to remove dead code. Which default optimization is doing this? How can I explicitly disable it to generate the error?
#include <iostream>
using namespace std;
class Base{
public:
int x;
string name;
void set(int val){ x = val;};
int get(){ return x;}
void Init();
};
int main() {
Base base;
base.set(10);
cout << base.get() << endl;
return 0;
}
EDIT1: Here Init() function is not defined and neither used anywhere. So, I expected compiler to complain about this not defined. But don't see any error/warning.
Thanks in advance.
Generally the linker will only produce errors for undefined symbols that are used. As you never call Init there is no error.
Looks like compiler is smart enough to remove dead code.
The compiler is not even "smart" here. There is no code using a function so that function is not needed to produce an executable program.
The function is not even "ODR used" so technically the compiler would be wrong to require a definition.
I decided to learn C++ DirectX, but whenever I compile/debug a code, even the simplest ones, i get the LNK2019: unresolved external symbol _WinMain# 16 referenced in function "int __cdecl_main(void)" (?invoke_main##YAHXZ) error and LNK1120.
I tested two different codes, one with class and other just int function alone:
#pragma once
class Main
{
public:
Main();
~Main();
};
Main::Main(int x)
{
}
Main::~Main()
{
}
int example()
{
return 0;
}
First and foremost you should do yourself a favour and learn C++ properly from a book. Bjarne Stroustrup, the designer and implementer of C++, has a great book which will teach you lots: Programming: Principles and Practice using C++
Your program cannot link because there is no main() function, which is required.
As Steephen has pointed out already in his answer, you can change your program so it includes at least the following:
int main()
{
return 0;
}
It looks like you were trying to substitute main() with example(), but your program and and C++ program needs to have a main(), as it's the main entry point of your program. You might also like to read http://www.cplusplus.com/doc/tutorial/program_structure/
To keep you class definition intact do following changes
Main::Main() // you don't need a parameter for your constructor
{
}
int main() //Not int example() because you need a main for your program
{
return 0;
}
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 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
This is a small program :
#include <iostream>
using namespace std;
int main() {
f();
system("pause");
}
void f() {
static int x = 20 ;
class tester {
public :
tester() {
cout << x ;
}
} x1;
}
The error that i get here is :error C3861: 'f': identifier not found
If i place the function f above main I will get the desired output.
Why it is so ?
I was told that program execution begins at main. According to this the code should run in the first case also.
How does the compiler start reading the program?
The beginning of the compilation and the beginning of the execution of the program are two different things.
The execution starts from the main.
The compilation begins from the beginning of the file; the compiler don't "jump around" the file to find the needed pieces, but it reads the input in a linear fashion (I suspect that this related, among the other things, to the fact that the C++ grammar is really complicated).
When the compiler is at some point in parsing the file, it only knows what has been declared/defined up to that point1.
Because of this, function prototypes (and non-defining declarations in general) have been invented: the prototypes of all the functions defined in the file are put at the beginning of the file, typically after the #include directives or in a separated include file. The prototypes tell to the compiler that such functions will be defined later, and what is the function signature (i.e. name, parameters, return value).
The prototype is made as a normal function, but without the body, which is replaced by a semicolon2. For example, in your code you would write
void f();
before the main.
IIRC there are some relaxations to this rule that allow the compiler to "wait" for some declarations to make some template magic work, but this is not relevant here.
In a prototype is also common not to write the names of the parameters, leaving just their type (this can be done also in function definitions, but it doesn't make much sense there unless you have a formal parameter you don't use). Still, I prefer to leave the parameter names there as a form of documentation.
I was told that program execution begins at main.
And that's exactly the point.
The compiler starts from main, and then sees a call to f(), which it has not encountered so far (as it is defined afterwards), so it does not know what to do with it.
If you want to define f after main you can place a function prototype before, such as
#include <iostream>
using namespace std;
void f(); // <--- This tells the compiler that a function name f will be defined
int main() {
f();
system("pause");
}
void f() {
static int x = 20 ;
class tester {
public :
tester() {
cout << x ;
}
} x1;
}
To be able to call a function it must have been declared at some earlier point in the code. This is just a rule of the language designed to help compilers.
You can declare the function earlier with e.g.
void f();
...and then define it after main as you have done.
The compiler starts at the top and reads down to the bottom.
you'll need to have something like:
#include <iostream>
using namespace std;
void f();
int main() {
f();
system("pause");
}
void f() {
static int x = 20 ;
class tester {
public :
tester() {
cout << x ;
}
} x1;
}
No, the compiler needs to see at least a declaration of f() before it is used. A c(++) code file is a simple text file and must be read from begin to end by the compiler.
During the compilation process, when the compiler is evaluating main() it needs to know what f() is in advance to be able to generate the correct assembly code to call this function. That's why you need to put it before main() in this case.
As an alternative you can declare the prototype of f() before main() so the compiler knows it's a local function declared somewhere else on your file:
void f(); // prototype
int main()
{
// .. code ..
}
void f() // implementation of f()
{
// .. code ..
}