这些文件会从较大的程序中缩短。
我在使用c ++编译这些文件时遇到问题,我不能完全确定问题是什么。我已经更改了函数的参数以包含变量course_holder和course_count,但这会增加更多错误。
我做错了什么?
struct_header.cpp
#ifndef FILENAME_H
#define FILENAME_H
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
struct course_struct
{
string course_ID;
};
#endif
func_proto.h
//func_proto.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void add_course(course_struct (& array_course)[10][100]);
#endif
functions.cpp
// functions.cpp
#include "struct_header.h"
#include "func_proto.h"
void add_course(course_struct (& array_course)[10][100])
{ // start f1
cout << "ENTER COURSE ID> ";
cin >> course_holder;
array_course[course_count][0].course_ID = course_holder; // course ID = user input
cout << course_holder;
course_count++; // increment for the next time this function is run
return;
}// end f1
main_funct.cpp
#include "struct_header.h"
#include "func_proto.h"
course_struct array_course[10][100];
int course_count = 0;
string course_holder;
int main()
{
add_course(array_course);
return 0;
}
和错误
functions.cpp: In function ‘void add_course(course_struct (&)[10][100])’:
functions.cpp:8:9: error: ‘course_holder’ was not declared in this scope
functions.cpp:10:15: error: ‘course_count’ was not declared in this scope
func_proto.h:5:33: error: variable or field ‘add_course’ declared void
func_proto.h:5:50: error: ‘array_course’ was not declared in this scope
func_proto.h:5:62: error: ‘course_struct’ was not declared in this scope
答案 0 :(得分:0)
您需要在course_count
course_holder
和functions.cpp
extern int course_count;
extern string course_holder;