Author Topic: Why won't this C++ program compile?  (Read 870 times)

Why won't this C++ program compile?
« on: April 19, 2016, 07:15:12 PM »
Quote
#include <iostream>
using namespace std;

bool accept()

{
cout << "Do you want to proceed (y or n)?\n"; // write question
char answer = 0;
cin >> answer; // read answer
if (answer == 'y') return true;
return false;
}

I am learning to program C++ and I am learning about loops. Why won't this program compile? Here is the error message I am getting:

Quote
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

If anyone here is fluent in C++ I would appreciate help.
 
The fireworks in my head don't ever seem to stop

Re: Why won't this C++ program compile?
« Reply #1 on: April 19, 2016, 08:59:05 PM »
I thought I identified the problem, I saved it with .cpp rather than .cc. I tried to compile it again and got,

Quote
/usr/lib/gcc/x86_64-redhat-linux/5.3.1/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
The fireworks in my head don't ever seem to stop

Re: Why won't this C++ program compile?
« Reply #2 on: April 19, 2016, 10:01:48 PM »
Every C++ program needs a main() function:

int main() {
    cout << "Hello, world!\n";
    return 0;
}

Offline Baruch

Re: Why won't this C++ program compile?
« Reply #3 on: April 20, 2016, 06:47:57 AM »
I always liked to use a working example, when I was learning.  Then make lateral modifications to get what my goal was.  That way, all the bits are there for the particular language and compiler and run-time environment.  As soon as it broke, then the last change I made is where the error is.

My first C language experience was negative, because the compiler sucked.  In theory it was supposed to ignore white space, but it didn't.  So I removed all the white space, then put it all back in.  Probably couldn't tell a single space from a one-space tab ;-)
שלום

Re: Why won't this C++ program compile?
« Reply #4 on: April 20, 2016, 07:48:28 AM »
Every C++ program needs a main() function:

int main() {
    cout << "Hello, world!\n";
    return 0;
}


Thank you very much, this worked.
The fireworks in my head don't ever seem to stop