对于一个项目,我们的教授为我们提供了一个.cpp文件和三个包含Class定义的.h文件。该项目的目标是为这些类构造实现.cpp文件。
我知道using namespace std;
是一种不好的做法,应该不惜一切代价避免,但是老师在完成项目时坚持了三个要点:
我的问题是,我收到一些与他在一个类中对forward_list<>
的定义有关的错误。这些错误对我来说毫无意义...在过去的几个小时里,我一直在挠头,同时尝试纠正这些错误:
1>Animation.cpp
1>AnimationManager.cpp
1>c:\users\ali-g\desktop\cst8219\assignment2\assignment2\animation.h(7): error C2143: syntax error: missing ';' before '<'
1>c:\users\ali-g\desktop\cst8219\assignment2\assignment2\animation.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ali-g\desktop\cst8219\assignment2\assignment2\animation.h(7): error C2238: unexpected token(s) preceding ';'
1>Ass2.cpp
1>Generating Code...
1>Done building project "Assignment2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这是他提供的.h文件,其中包含Animation类定义:
//Animation.h
#pragma once
class Animation
{
string animationName;
forward_list<Frame> frames;
public:
Animation(string name) :animationName(name) {}
~Animation() {}
void EditFrame();
void DeleteFrame();
friend istream& operator>>(istream&, Animation&);// Add a Frame as in cin >> A;
friend ostream& operator<<(ostream&, Animation&);// output the Frames as in cout << A;
};
这是我从Animation Class实现的.cpp文件:
//Animation.cpp
#include <iostream>
#include <string>
#include <forward_list>
using namespace std;
#include "Frame.h"
#include "Animation.h"
void Animation::EditFrame() {
}
void Animation::DeleteFrame() {
}
istream& operator>> (istream& in, Animation& animation) {
return in;
}
ostream& operator<< (ostream& out, Animation& animation) {
return out;
}
他提供的.h文件包含AnimationManager类的定义:
// AnimationManager.h
#pragma once
class AnimationManager
{
string managerName;
vector<Animation> animations;
public:
AnimationManager(string name) :managerName(name) {}
~AnimationManager() {}
void EditAnimation();
void DeleteAnimation();
friend istream& operator>>(istream&, AnimationManager&);// add an Animation
friend ostream& operator<<(ostream&, AnimationManager&);// output the Animations
};
这是我的AnimationManager类的实现文件:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include "Frame.h"
#include "Animation.h"
#include "AnimationManager.h"
void AnimationManager::EditAnimation() {
}
void AnimationManager::DeleteAnimation() {
}
istream & operator>>(istream &in, AnimationManager &manager){
string name;
cout << "Please enter Animation Name: ";
cin >> name;
//resize vector array to accommodate a new Animation
manager.animations.resize(manager.animations.size() + 1);
manager.animations.push_back(Animation(name));
}
ostream & operator<<(ostream &out, AnimationManager &manager){
}
我还将为其他随附的头文件(Frame.h)包含.h&.cpp文件,因为它可以帮助您发现我的(最可能是愚蠢的)错误。
“ Frame.h”:
// Frame.h
#pragma once
class Frame{
char* frameName;
double duration;
public:
Frame(char*, double);
Frame(const Frame&);
~Frame();
Frame& operator=(const Frame&);
friend ostream& operator<<(ostream&, Frame&);
};
“ Frame.cpp”:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include "Frame.h"
Frame::Frame(char* name, double dur) {
//Set frame name
frameName = new char[strlen(name) + 1];
strcpy(this->frameName, name);
//set duration
duration = dur;
}
Frame::Frame(const Frame& frame) {
//copy name from passed frame
frameName = new char[strlen(frame.frameName) + 1];
strcpy(frameName, frame.frameName);
//copy duration
duration = frame.duration;
}
Frame::~Frame() {
//free the space allocated for frameName
if (frameName != nullptr) {
delete[] frameName;
}
}
Frame& Frame::operator= (const Frame& frame) {
//create new Frame using given frame information
Frame copyFrame = Frame(frame.frameName, frame.duration);
//return reference to new frame.
return copyFrame;
}
ostream& operator<< (ostream& out, Frame& frame) {
//add frameName and duration to ostream.
out << "\tframeName - " << frame.frameName << "; ";
out << "duration - " << frame.duration << endl;
//return ostream
return out;
}
对于解决此问题的任何帮助,我将不胜感激。预先感谢!