onepunchman
onepunchman

Reputation: 77

Separating a class into cpp and header file (C++)

im new to C++ language. So I was assigned to split an existing file into three source code: swap.h, swap.cpp and source3.cpp

Existing File:

#include <iostream>

void get_numbers (int&, int&);
void swap_values (int&, int&);
void show_results (int, int);

int main () {
   int first_num, second_num;

   get_numbers (first_num, second_num);
   swap_values (first_num, second_num);
   show_results (first_num, second_num);
   return 0;
}

void get_numbers (int& input1, int& input2) {
   using namespace std;
   cout << "Enter two integers: ";
   cin >> input1 >> input2;
}

void swap_values (int& variable1, int& variable2) {
   int temp;
   temp = variable1;
   variable1 = variable2;
   variable2 = temp;
}

void show_results (int output1, int output2) {
   using namespace std;
   cout << "In reverse order the numbers are: "
        << output1 << " " << output2 << endl;
}
  1. swap.h contains function prototypes

  2. swap.cpp contains function implementations

  3. source3.cpp contains the main function

for swap.h:

#pragma once
#ifndef swap_h
#define swap_h

void get_numbers(int&, int&);
void swap_values(int&, int&);
void show_results(int, int);
#endif

for swap.cpp

    #include <iostream>

    void get_numbers(int& input1, int& input2) {
       using namespace std;
       cout << "Enter two integers: ";
       cin >> input1 >> input2;
    }

    void swap_values(int& variable1, int& variable2) {
        int temp;
        temp = variable1;
        variable1 = variable2;
        variable2 = temp;
    }

    void show_results(int output1, int output2) {
       using namespace std;
       cout << "In reverse order the numbers are: "
       << output1 << " " << output2 << endl;
    }

for source3.cpp:

    #include "stdafx.h"
    #include "swap.h"
    int main()
    {
       int first_num, second_num;

       get_numbers(first_num, second_num);
       swap_values(first_num, second_num);
       show_results(first_num, second_num);
       return 0;
    }

When I debug the program, it says: "Unable to start program 'C:\User......' The system cannot find the file specified. What am I doing wrong?

Upvotes: 0

Views: 127

Answers (2)

Earinor
Earinor

Reputation: 35

If what you provided is the whole code, you didn't include swap.h in swap.cpp. Therefore you have the definition of the functions, but no declaration. Although I would imagine another error or at least a warning here. Try to fix that.

If it doesn't work, try building the Release Version. Does it compile? Does it start? And when it is starting, does it do anything? If what I mentioned before is the problem, I would expect the program to just run to the end, without doing anything.

If the problem lies with swap.h in the main File, make sure it is in the same location, or the include paths point to the directory which contains it. Same goes for stdafx.h

Also, you don't need #pragma once and #ifndef #define and #endif. Get rid of either of those, I recommend using #ifndef #define and #endif, because #pragma once is not supported everywhere. But for you it shouldn't matter.

Upvotes: 0

&#199;ağrı Uslu
&#199;ağrı Uslu

Reputation: 77

Since your code compiles successfully, but cannot be started, you probably have problems related to your debugging environment.

Also, you don't need #ifdef, #define, and #endif once you have #pragma once.

Upvotes: 1

Related Questions