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 7020801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:21:56+00:00 2026-05-27T23:21:56+00:00

I have a namespace in which I’d like to define a class. The class

  • 0

I have a namespace in which I’d like to define a class. The class is rather complex so I’d rather define it in a separate header file, but even the simplest code gives me an “undefined reference” error.

main.cpp

#include <iostream>
namespace A {
    #include "C.hpp"
}

int main()
{
    A::C foo;
    std::cout << foo.member << std::endl;
    return 0;
}

C.hpp

class C {
    public:
     C();
     int member;
}

C.cpp

C::C()
{
    this->member = 10;
}

When I run g++ C.cpp main.cpp I get “main.cpp:(.text+0x10): undefined reference to `A::C::C()'” error. I suppose that it’s the C::C() definition of the constructor that is somehow wrong, but I’m uncertain how to fix it.

  • 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-27T23:21:57+00:00Added an answer on May 27, 2026 at 11:21 pm
    namespace A {
        #include "C.hpp"
    }
    

    This is a very odd thing to do.

    This places everything declared in the header inside a namespace called A; specifically, it gives you a declaration of A::C, which is a different class to the ::C you get when you include the header without a surrounding namespace.

    You have provided a definition for ::C::C(); but main.cpp requires a definition for A::C::C(), since that is the class it uses. That’s not defined anywhere, hence the error.

    Either put C properly into namespace A by moving the namespace to the header file (and fix C.cpp to use that namespace), or get rid of namespace A.

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

Sidebar

Related Questions

I set my DataContracts namespace to which removed one namespace but I have to
I have a namespace ABC, which has one form class, F and an ordinary
Using VB.net, I have a namespace which I'd like to rename for the future.
I'd like to have arrays in my namespace which all functions in the namespace
Within our namespaced class files, there're various classes which have the namespace prefixed with
I have a small namespace containing some type definitions, which I use to make
I have a helper assembly which includes a function to identify object types: namespace
If I have a namespace like: namespace MyApp.Providers { using System; using System.Collections.Generic; using
I have two files Sample.cpp and Main_file.cpp. Sample.cpp has only one namespace n1 which
I have an enum in a namespace and I'd like to use it as

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.