다음 프로그램이 있습니다. 그것은 객체 지향이며, 구조체 '배열'을 가지고 있습니다. (어떤 구조체를 사용해야하는데, vector.h는 계산하지 않습니다) 그래서 어떤 객체를 저장합니다. Class 함수와 구조는 M.cpp (main)에서 잘 작동하지만 controller.cpp에서 호출하려고하면 참조 오류가 발생합니다.다른 모듈의 함수 호출 - 참조 오류
ListStruct.h :
template <class T>
struct Array{
int days;
T * M;
Array(int size) : days(size), M(new T[size])
{
}
~Array()
{
delete[] M;
}
};
void currentDay();
template <class T>
void add(int,int,Array<T> &);
template <class T>
void dummyData(Array<T> &);
ListStruct.cpp
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include "ListStruc.h"
#include "Expe.h"
using namespace std;
int currDay;
void currentDay(){
time_t t = time(0); // get time now
struct tm * now = localtime(& t);
::currDay = now->tm_mon + 1;
}
void add(int cant, int type,Array <Expe> &A){
//Adds to current day the amount to a specific type
int newVal;
newVal = A.M[currDay].getObj(type);
newVal += cant;
A.M[currDay].editObj(type,newVal);
}
void dummyData(Array <Expe> &A){
for(int i=0; i<31; i++){
A.M[i].Expe::setObj((i*3),(i*1),(i*6),(i*2),(i*4),(i*5));
}
}
M.cpp - 프로그램의 주요 기능 :
#include <iostream>
#include "Expe.h"
#include "ListStruc.h"
#include "Controller.h"
using namespace std;
int main(){
//Main function of the program. no pre/ post condition.
Array <Expe> A(31); // Work space
Array <Expe> B(31); // Backup space
cout<<&A; // testing for allocation
cout<<&B;
Expe a;
a.setObj(5,5,5,5,5,5); // checking class functions
a.printObj();
A.M[1]=a;
A.M[1].printObj();
//check dummy FOR-, WORKS!
for(int i=0; i<31; i++){
A.M[i].Expe::setObj((i*3),(i*1),(i*6),(i*2),(i*4),(i*5));
}
a.editObj(3,100);
a.printObj(); // check objects calling, WORKS!
cout<<"Obj A.[10]:";
A.M[10].printObj();
cout<<endl<<"Obj A.[29]:";
A.M[29].printObj();
dummyData(A); ///////ERROR/////////
ERROR :
D:\c++\Begin\Lab3-5_OOP\Debug/../M.cpp:44: undefined reference to `void dummyData<Expe>(Array<Expe>&)'
나는 내가 생각할 수있는 모든 것을 시도했다 ... 인터넷을 서핑했고 여전히 그 참조 오류의 이유를 찾을 수 없었다.
아는대로 템플릿 기능을 완전히 전문화 한 경우 손을 .cpp 파일 안에 숨기십시오. 내가 잘못? :) – parallelgeek
그리고 나는 티가 어떻게 될지에 따라 템퍼링되기를 원한다고 가정 해 봅시다. 예를 보여 주시겠습니까? –
@parallelgeek : 네, 그렇게 할 수는 있지만, 아무런 의미가 없습니다. 오버로딩은 동일한 결과를보다 간단하게 제공합니다. –