#include<iostream.h>
#include<conio.h>
int main(void)
{
    void hello()
    {
        cout<<"HI";
    }
    getch();
    return 0;
}

I am getting an error at line 6 stating that declarartion syntax error. What do I do?

Recommended Answers

All 2 Replies

1) Get an up to date compiler (many are free like this one:

http://sourceforge.net/projects/orwelldevcpp/

2) Do not use conio.h ... or getche ... to keep code portable

3) Define your functions outside of main function ... before called

4) See example of how modern C++ looks ...

// hello.cpp //

#include <iostream>


void hello()
{
    std::cout<<"HI";
}


int main(void)
{
    hello();

    // keep 'window open' until 'Enter' key is pressed 
    std::cout << "\n\nPress 'Enter to continue/exit ... " << std::flush;
    std::cin.get();

    return 0;
}

You are not allowed to define a function in a function and that is what the compiler is complaining about. There are two solutions to this. You could move the function out of main and put it in the global scope or you can change the function into a lambda which is allowed at the local scope.

#include<iostream>
// global function decleration
void hello()
{
    std::cout << "HI";
}

int main()
{ 
    std::cin.get(); // used to pause program.  press enter to continue
    return 0;
}

or

int main()
{ 
    auto func = []() { std::cout << "HI"; };
    func();
    std::cin.get(); // used to pause program.  press enter to continue
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.