如何在标头中声明(或定义)函数的问题

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

本文关键字:定义 函数 问题 声明      更新时间:2023-10-16

我正在努力实现我在c ++程序的头文件中定义的函数。我想我误解了它是如何工作的,但大多数在网上阅读的内容都没有把它说得足够清楚,让我的牡丹大脑理解。

我正在尝试创建一个'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;
}

这是我定义sort_name函数courses_functions.cpp

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标题:

#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中实现它们。

这是一个没有.cpp简化的工作示例。只有Courses.hmain.

有了.cpp你的程序应该是这样的:

课程.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实现中:

当然.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