Thread: Question about C++ Splitting your Code into Multiple Files

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    2

    Question about C++ Splitting your Code into Multiple Files

    hello guys! sorry if my question might seem dumb. I have anxiety and its hard for me to ask for help but i am a newbie learning c++ as my first language.
    today i looked up that you can actually split your code into multiple files to make it organised so i tried to make a very basic program but the main function still doesnt recgonize my function that I put in another file
    I believe I did all the includes and if someone can give me some light I really appreciate it >.<!

    my main.cpp is:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include "operator.hpp"
    
    
    using namespace std;
    
    
    int maint()
    
    
    {
        double c , d;
        
        char operatior;
        
        cout<<"enter first number" << endl;
        cin>>c;
        cout<<"enter second number" << endl;
        cin>>d;
        cout<<"choose your operator"<< endl;
        cin>> operatior;
        
        affiche_resultat (operatior,c,d); //simulates how a calculator works
        
        return 0;
    
    
    }
    operator.hpp code:
    Code:
     
    #ifndef OPERATOR_HPP_INCLUDED
    #define OPERATOR_HPP_INCLUDED
    
    
    void affiche_resultat(char op, double a, double b);
    
    
    #endif // OPERATOR_HPP_INCLUDED


    operator.cpp code
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include "operator.hpp"
    
    
    using namespace std;
    
    
    void affiche_resultat(char op, double a, double b)
    {
        switch(op)
        {
    
    
    
    
        case '+' :
                  cout<<"the result is"<< a+b << endl;
                  break;
        case '-' :
                  cout<<"the result is"<< a-b << endl;
                  break;
        case '*' :
                  cout<<"the result is"<< a*b << endl;
                  break;
        case '/' :
                  if (b=0)
                  {
                      cout<<"Never devision by 0"<< endl;
                  }
                  else
                  {
                      cout<<"the result is" << a/b <<endl;
                  }
                  break;
        default:
                  cout<<"error operator not valid"<<  endl;
    }
    }

    Edit Sorry forgot to mention the error i keep getting is basically telling me that it cant recognize the function I am using : undefined reference
    Last edited by roroprogram; 02-16-2021 at 12:35 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,540
    Which compiler / IDE are you using?

    If you're compiling from the command line, all you need is
    g++ main.cpp operator.cpp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    2
    oh idk how to make my own makefile yet but i figured it out , its just that the IDE i was using wasn't compiling all the files cos they werent in the same project
    I also corrected few mistakes >.<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Separating out code into multiple files
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 04-24-2011, 05:13 PM
  2. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 07:31 AM
  3. Splitting Code Into Multiple Files
    By pobri19 in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2008, 04:21 AM
  4. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  5. Splitting source code files
    By rocksteady in forum C Programming
    Replies: 2
    Last Post: 10-19-2007, 02:01 PM

Tags for this Thread