Kate Maryon Herrick
Kate Maryon Herrick

Reputation: 1

main() function not recognized

I am writing code for a class I'm taking. I can't post all my code without zeroing out my score for the project but here is the abbreviated code for my driver:

    #pragma once
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "Stack.h"

    using namespace std;

    namespace jack
    {
        int high(char a)
        {
            // My Code
        };

        bool isSameOrHigher(char top, char cur)
        {
            // My Code
        };

        int main()
        {
            // My Code
        };
    };

For some reason that I cannot figure out when I compile this code I get the following error:

LINK : fatal error LNK1561: entry point must be defined

Now, as far as I know this error should only happen if I don't have a main function, which you can see I do actually have. I have tried copying the code from this file into another project, I've tried separating my main function out into another cpp file all by itself (which caused more errors and didn't fix the entry point error), and I have tried re-installing Visual C++ express and starting completely from scratch. My teacher and I have checked all the code in this file before main() (and all the code in the Stack.h file I wrote and included) and there aren't any missing parentheses, semicolons, or any other punctuation. I don't know what else to even try. Thoughts?

Upvotes: 0

Views: 2317

Answers (5)

Jerry Coffin
Jerry Coffin

Reputation: 490358

You need to move main outside any namespace.

For anybody who cares about exactly what the standard has to say (§3.6.1/1):

A program shall contain a global function called main, which is the designated start of the program.

Edit: for those who also want the last word on what "global" means (§3.3.5/3 in C++03, §3.3.6/3 with nearly identical wording in C++11):

The outermost declarative region of a translation unit is also a namespace, called the global namespace. A name declared in the global namespace has global namespace scope (also called global scope). [...] Names with global namespace scope are said to be global.

Upvotes: 8

Govind Balaji
Govind Balaji

Reputation: 639

You should define main() only in global namespace, not inside any other namespaces.

Upvotes: 2

Tyler Jandreau
Tyler Jandreau

Reputation: 4335

Take your main out of the namespace

Upvotes: 5

Adam Batkin
Adam Batkin

Reputation: 53024

Take your main function out of the namespace. Technically your main is actually jack::main while it is inside the namespace.

Upvotes: 3

Thomas Matthews
Thomas Matthews

Reputation: 57728

Move the main function outside the namespace.

Upvotes: 3

Related Questions