• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

Mr.CodeHunter

Programming and Code Solutions

  • Home
  • C++
  • About Us
  • Contact Us
  • Privacy Policy

c++ expected a declaration [ SOLVED]

June 26, 2021 by Admin Leave a Comment

When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code.

Main reasons for errors are:

  1. Incorrect use/declaration inside namespace
  2. Statements are added out of scope
  3. Required statement need to add inside main/function
reasons for c++ expected declaration
reasons for c++ expected declaration

Solution-1 | Expected a declaration – When used namespace instead of class.

When developer is mostly worked in java/C# then possibly then make mistake in c++ code for namespace. If we check here given syntax is not correct for name space. For c++ we use public: for any functions.

Namespace related issue
Namespace related issue
namespace A {
// Expected declaration error
 public static void Print();
};

c++ expected a declaration error will come here. Here if we want to resolve error then instead of namespace we can use class. Inside Class A we can add public access specifier using colon(:) then we have to write methods. Here public static void print() is not valid in c++ so it will give error for your c++ code.

// Error resolved by using class and making function as public:
class A {
public:
	static void Print();
};

Solution-2 | Expected a declaration c++ – When statement is not inside function and added out of scope

Add code inside function scope
Add code inside function scope

Here at place of if condition it is giving us error that declaration is expected. If you see give code then you will realize that condition is not in any scope and if require inside some function.

#include <iostream>
using namespace std;

int main() {

	return 0;
}

// Expected a declaration error
if (true) {
	cout << "Hello";
}

Here we have used if statement out side main() and also it is not inside any function. So it is out of scope. We will either require to add given code inside any of function or we can add inside main function then only expected a declaration error will be resolved.

We are successfully able to resolve this error by adding our if condition logic inside void func() , now you can use this function call to execute your logic.

#include <iostream>
using namespace std;

int main() {
    func();
	return 0;
}

// Resolve by adding function
void func() {
	if (true) {
		cout << "Hello";
	}
}

Solution-3 | c++ Expected a declaration – When return statement is out side main

Here we have added return 0 outside main function. So it is giving us error for expected a declaration. Because return statement is require inside of any function. We can not write direct statement which is out of scope.

#include <iostream>
using namespace std;

int main() {
 // Return is added outside main
}

// expected a declarion - syntax error return
return 0;

For solving this issue you can add return inside your main function. If similar errors you are getting for other class/function then you can first check given statement is inside scope or it require to add in any function.

int main() {
// Resolving it 
 return 0;
}

CONCLUSION:

Whenever you get error for c++ expected a declaration then you can check that your error indicated code is inside function scope ? or you have used namespace improperly. Mostly by checking it you will find reason for error and can resolve easily by adding code inside scope.

SEE MORE:

Top 10 C++ IDE – you must try once

Filed Under: All C++, C++

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search here

Social Media

  • Facebook
  • YouTube

SEE MORE

Fibonacci sequence c++

Fibonacci Sequence c++ is a number sequence which created by sum of previous two numbers. First two number of series are 0 and 1. And then using these two number Fibonacci series is create like 0, 1, (0+1)=1, (1+1)=2, (2+1)=3, (2+3)=5 …etc Displaying Fibonacci Series in C++ ( without recursion) Output: From given output we […]

map c++

C++ Map [Learn by Example]

C++ map is part of Standard Template Library (STL). It is type of Associative container. Map in c++ is used to store unique key and it’s value in data structure. But if you want to store non-unique key value then you can use Multi Map in c++. Let us first understand in detail what is […]

how to copy paste in turbo c++ ?

There are many different C++ IDE are available but still many students are using Turbo c++ for learning c/c++ programming languages. During using Turbo c++ if you are beginner you will be confuse for how to copy and paste in turbo c++ or if you have already copy some content and you want to paste […]

C++

return 0 c++

There are two different scenario return statement is used inside c++ programming. We can use return 0 c++ inside main() function or other user defined functions also. But both have different meanings. return 0 c++ used inside Main function return 0 c++ used inside other user defined function What is meaning of return 0 and […]

C++

c++ expected a declaration [ SOLVED]

When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code. Main reasons for errors are: Incorrect use/declaration inside namespace Statements are added out of scope Required statement need to add inside main/function Solution-1 | Expected a […]

C++

c++ cannot open source file “errno.h” [SOLVED]

Normally you will face c++ cannot open source file “errno.h” in MS Visual Studio c++ projects. These are some solutions to remove opening errors for “errno.h” file. I got the errors to go away by installing the Windows Universal CRT SDK component, which adds support for legacy Windows SDKs. You can install this using the […]

Footer

DISCLAIMER

The information contained on https://www.mrcodehunter.com is for general information purposes only. We assumes no responsibility for errors or omissions in the contents on the Service.

SITEMAP XML

Sitemap

Recent

  • Fibonacci sequence c++
  • C++ Map [Learn by Example]
  • how to copy paste in turbo c++ ?
  • return 0 c++
  • c++ expected a declaration [ SOLVED]

Search

Copyright © 2025 ยท Mr Code Hunter