cVos
cVos

Reputation: 27

Trouble with how to declare (or define) a function in a header

I'm struggling with implementation of a function that I define in my header file of my c++ program. I think I am misunderstanding how it works, but most reading on the web doesn't spell it out clear enough for my peon brain to understand.

I'm trying to make a 'sort_name' function that sorts an array of private classes based on the c-string "name" when the function is invoked.

Unfortunately I keep running into errors when trying to use it.

Here's my courses_main.cpp's main function:

int main()
{
    Course* courses[10] = {};
    int selection;

    int size = 0;
    do
    {
        selection = menu();

        if (selection == 1)
        {
            if (size < 10)
                add(courses, size);
            else
                std::cout << "\nUnable to add more classes.";
        }
        else if (selection == 2)
        {
            edit(courses, size);
        }
        else if (selection == 3)
        {

        }
        else if (selection == 4)
        {
            sort_name(courses, size);
            for (int i = 0; i < size; i++)
            {
                courses[i]->display();
            }
        }
        else if (selection == 5)
        {

        }
        else if (selection == 6)
        {

        }
        else if (selection == 7)
        {
            break;
        }
        else
        {
            std::cout << "\nInvalid selection.";
        }
    } while (selection != 7);

    std::cout << "\nPress any key to exit.";
    (void)_getch();
    return 0;
}

Here is my courses_functions.cpp where I define the sort_name function:

void swap_ptrs(Course*& pt1, Course*& pt2) //Passes the pointers by reference
{
    Course* tmp = pt1;
    pt1 = pt2;
    pt2 = tmp;
}

void Course::sort_name(Course* co_ptr[], int size) //has to be apart of the class (Course::) to have access to the name data
{
    bool swap;

    do
    {
        swap = false;
        for (int i = 0; i < size - 1; i++)
        {
            if (strcmp(co_ptr[i]->name, co_ptr[i + 1]->name) > 0) //We're now comparing and swapping pointers
            {
                swap_ptrs(co_ptr[i], co_ptr[i + 1]);
                swap = true;
            }
        }
    } while (swap);
}

And here is my courses.h header where I define(?) the function:

#ifndef COURSE_H
#define COURSE_H

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <ctime>
#include <fstream>
#include <cstdlib>


class Course
{
private:
    char name[10] = "", grade;
    int units;
public:
    Course()
    {
        name;
        grade;
        units = 0;
    }

    void read() //Initializes course and collects information from user
    {
        std::cout << "\nEnter course name: ";
        std::cin.getline(name, 10, '\n');
        std::cout << "\nEnter number of units: ";
        std::cin >> units;
        std::cout << "\nEnter grade received: ";
        std::cin >> grade;
        std::cin.ignore();
    }

    void display() const //Displays course to user
    {
        std::cout << name << ' ' << units << ' ' << grade << std::endl;
    }

    ~Course() //Destructor frees allocated dynamic memory
    {
        std::cout << "\nDeleting any dynamically created object";
    }

    void sort_name(Course* co_ptr[], int size);
};

#endif // COURSE_H

I'm not understanding much about classes outside of how they're extremely similar to structs, so any direction would be welcome thanks!

Upvotes: 0

Views: 98

Answers (1)

anastaciu
anastaciu

Reputation: 23792

A better code organization would be to declare the functions in the .h file and implement them in the.cpp.

Here is a working sample without .cpp for simplification. Only Courses.h and main.

With .cpp your program should be somethong like this:

Course.h

#ifndef COURSE_H
#define COURSE_H

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;  //<-- for test pusposes, you should use std:: scope

class Course
{
private:
    string name;
    int units, grade;

public:
    Course(); //<-- the code you have inside the constructor, only units = 0, 
              // does somenthing, you should initialize all the members.
    Course(string name);
    void read();
    void display() const;
    ~Course(); //<-- to dealocate dynamic memory you need to really dealocate it with delete.
    string getName() const;
};

#endif // COURSE_H

And in your .ccp the implementation:

Course.cpp

#include "Course.h"

Course::Course(){ /*do stuff*/ }

Course::Course(string name) : name(name) { /*do stuff*/ } //<-- initializing name here

void Course::read() {/*do stuff*/ }

void Course::display() const {/*do stuff*/ }

Course::~Course() {/*do stuff*/ }

string Course::getName() const { return name; }

For sorting you don't need anything fancy you have sorting tools and data structures in the C++ libraries that make your job easy, like vector for objects container and sort for sorting.

Main

#include "Course.h"

 bool sorting(Course course1, Course course2) { //conditional function for sort (#include <algorithm>)
    return course1.getName() < course2.getName();
}

int main() {

    vector<Course> courses = { Course("zed"), Course("albert")}; // adding courses
    courses.push_back(Course("mary")); // adding some more
    courses.push_back(Course("john"));
    courses.push_back(Course("ana"));
    courses.push_back(Course("charles"));

    sort(courses.begin(), courses.end(), sorting); //<-- sorting

    for (Course c : courses) {
        cout << c.getName() << " ";
    }
}

Output:

albert ana charles john mary zed

Upvotes: 1

Related Questions