Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6558403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:11:43+00:00 2026-05-25T13:11:43+00:00

My code is like below, when I compile it , I receive this error

  • 0

My code is like below, when I compile it , I receive this error :

two.cpp:5: error: expected constructor, destructor, or type conversion before ‘=
‘ token

#include <iostream>
using namespace std;
namespace a1{
    int a=3;
    a=4;
}

int main(){
    cout << a1::a<<endl;
}

I encountered this problem when I defined a namespace in two files, in the second file, I can’t assign a value to a variable which defined in the first file.

I am learning Beginning ANSI C++ , and can’t find any information about this in the book.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:11:45+00:00Added an answer on May 25, 2026 at 1:11 pm

    The purpose of namespaces is to avoid conflicting names. So surround your variable and class declarations with namespaces. But namespaces by themselves don’t provide the scaffolding for running code. What is going on with your a=4; statement ? Where is that supposed to execute ? You need to put it in a function or method, not in a namespace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this error while trying to compile the below code. I would like
This has been bothering me lately- when I use some code like below to
I have a code like this below, which gives me a $link that equals
Just a general c++ curiosity: This code below shouldn't compile because it's impossible to
I have two strange cases where it seems like code should compile, but it
I saw a below code: Map(1 -> one, 2 -> two) map _._1 this
When I try to compile the piece of code below, I get this warning:
I met an interesting issue about C#. I have code like below. List<Func<int>> actions
The code looks like below: namespace Test { public interface IMyClass { List<IMyClass> GetList();
I constantly find myself writing similar code like the example below: if (object[Object Name]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.