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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:58:20+00:00 2026-06-12T10:58:20+00:00

I have two files: File1.cpp File2.cpp File1 is my main class which has the

  • 0

I have two files:

File1.cpp
File2.cpp

File1 is my main class which has the main method, File2.cpp has a class call ClassTwo and I want to create an object of ClassTwo in my File1.cpp

I compile them together by

g++ -o myfile File1.cpp File2.cpp

but when I try to create by

//create class two object

ClassTwo ctwo;

It doesn’t work.

Error was

ClassTwo was not declared in this scope.

This is my main.cpp

#include <iostream>
#include <string>
using namespace std;

int main()
{
//here compile error - undeclare ClassTwo in scope.
ClassTwo ctwo;

//some codes
}

Here is my File2.cpp

#include <iostream>
#include <string>

using namespace std;

class ClassTwo
{
private:
string myType;
public:
void setType(string);
string getType();
};


void ClassTwo::setType(string sType)
{
myType = sType;
}

void ClassTwo::getType(float fVal)
{
return myType;
}

Got respond of splitting my File2.cpp into another .h file but if i am declaring a class, how do i split it into another .h file as i need to maintain the public and private of the variable(private) and functions(public) and how do i get ClassTwo ctwo to my File1.cpp at main method

  • 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-06-12T10:58:21+00:00Added an answer on June 12, 2026 at 10:58 am

    What is the basic problem in your code?

    Your code needs to be separated out in to interfaces(.h) and Implementations(.cpp).
    The compiler needs to see the composition of a type when you write something like

    ClassTwo obj;
    

    This is because the compiler needs to reserve enough memory for object of type ClassTwo to do so it needs to see the definition of ClassTwo. The most common way to do this in C++ is to split your code in to header files and source files.
    The class definitions go in the header file while the implementation of the class goes in to source files. This way one can easily include header files in to other source files which need to see the definition of class who’s object they create.

    Why can’t I simply put all code in cpp files and include them in other files?

    You cannot simple put all the code in source file and then include that source file in other files.C++ standard mandates that you can declare a entity as many times as you need but you can define it only once(One Definition Rule(ODR)). Including the source file would violate the ODR because a copy of the entity is created in every translation unit where the file is included.

    How to solve this particular problem?

    Your code should be organized as follows:

    //File1.h

    Define ClassOne 
    

    //File2.h

    #include <iostream>
    #include <string>
    
    
    class ClassTwo
    {
    private:
       string myType;
    public:
       void setType(string);
       std::string getType();
    }; 
    

    //File1.cpp

    #include"File1.h"
    
    Implementation of ClassOne 
    

    //File2.cpp

    #include"File2.h"
    
    void ClassTwo::setType(std::string sType)
    {
        myType = sType;
    }
    
    void ClassTwo::getType(float fVal)
    {
        return myType;
    } 
    

    //main.cpp

    #include <iostream>
    #include <string>
    #include "file1.h"
    #include "file2.h"
    using namespace std;
    
    int main()
    {
    
        ClassOne cone;
        ClassTwo ctwo;
    
        //some codes
    }
    

    Is there any alternative means rather than including header files?

    If your code only needs to create pointers and not actual objects you might as well use Forward Declarations but note that using forward declarations adds some restrictions on how that type can be used because compiler sees that type as an Incomplete type.

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

Sidebar

Related Questions

Static variable has file scope. Say I have two following files: file1.h file1.cpp file2.h
I have a main function in A.cpp which has the following relevant two lines
I have two C++ files, say file1.cpp and file2.cpp as //file1.cpp #include<cstdio> void fun(int
I have two files Sample.cpp and Main_file.cpp. Sample.cpp has only one namespace n1 which
Background I have a project named PersonLibrary which has two files. Person.h Person.cpp This
I have two files called file_utils.h and file_utils.cpp which contain some methods and variables
I have two FASTA files: file1.fasta >foo ATCGGGG >bar CCCCCC file2.fasta >qux ATCGGAAA What
I have two files. file1 has the data like belowing containing only one column.
I have two files 1 - index.php 2 - main.php index.php call to main.php
I have two files: // event_test_delete.cpp #include <event.h> int main() { event_base* ev; ev

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.