关于如何在 header 中声明(或定义)函数的问题

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

我正在努力实现我在我的 c++ 程序的 header 文件中定义的函数。我想我误解了它是如何工作的,但是网上的大多数阅读都没有把它拼写得足够清楚,我的 peon 大脑无法理解。

我正在尝试制作一个 'sort_name' 函数,该函数在调用函数时根据 c-string "name" 对私有 类 数组进行排序。

不幸的是,我在尝试使用它时一直 运行 出错。

这是我的 courses_main.cpp's 主要功能:

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;
}

这是我的 courses_functions.cpp 定义 sort_name 函数的地方:

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);
}

这是我的 courses.h header 我定义(?)函数的地方:

#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

除了它们与结构极其相似之外,我对 类 了解不多,所以欢迎任何指导,谢谢!

更好的代码组织是在 .h 文件中声明函数并在 .cpp 中实现它们。

这里有一个working sample为了简化没有.cpp。只有 Courses.hmain.

使用 .cpp 你的程序应该是这样的:

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

并且在您的 .ccp 实施中:

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; }

对于排序,您不需要任何花哨的东西,C++ 库中的排序工具和数据结构可以让您的工作变得轻松,例如 vector 用于对象容器,sort 用于排序。

主要

#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() << " ";
    }
}

输出:

albert ana charles john mary zed