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

  • SEARCH
  • Home
  • 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 163083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:30:18+00:00 2026-05-11T11:30:18+00:00

I am still learning C++, and I have never really created my own namespaces

  • 0

I am still learning C++, and I have never really created my own namespaces before. I was experimenting with them and while I got most things to work, there’s one thing that I still can’t seem to do. I would like to be able to call a static method within a class without typing something like NameOfClass::method. Here is what I thought the code should look like, but it fails to compile:

File A.h,

namespace Test {     class A     {         public:             static int foo() { return 42; }     }; } 

File main.cpp,

#include <iostream>  #include 'A.h'  using namespace std; using namespace Test::A;  int main() {     cout << foo() << endl;      return 0; } 

The compiler gives me:

main.cpp:6: error: ‘A’ is not a namespace-name main.cpp:6: error: expected namespace-name before ‘;’ token main.cpp: In function ‘int main()’: main.cpp:10: error: ‘foo’ was not declared in this scope 

Is it possible to do what I am trying to do without typing A::foo?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T11:30:19+00:00Added an answer on May 11, 2026 at 11:30 am

    There is no way around it you need to specify the class name for static methods.

    using namespace Test; 

    Then:

    int answerToEverything = A::foo(); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Still learning JSP Web Applications here. I have been doing this for a while
Hey guys, I'm still learning C and have created a virtual cash register. One
I started learning how to use Selenium today. I have never used it before.
I'm still learning php and have run up against what should be a relatively
Sorry guys, didn't know how to word this one. Still learning jquery and have
I have a simple request(still learning javascript). I have a title div and 3
I am still learning socket programming (using Perl) but I have both options (
Ok I am still learning this... I have Googled and done some research but
I have only started learning python recently. I would still be considered a beginner.
im currently learning python (in the very begining), so I still have some doubts

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.