模板错误c ++

时间:2016-10-12 16:36:59

标签: c++ templates

我在模板方面遇到了一些麻烦。由于某种原因,我的模板本身看起来很好,但是当我因某种原因尝试使用它时,即使我知道我正确使用它也会出错。该模板用于一个简单的链表,用于保存子Shape类和CustomShape类。此代码适用于学校,因此如果您只能评论与模板相关的错误,那将是很好的。我已经阅读了关于这个主题的其他帖子,但似乎没有任何帮助/解决我的问题。

使用Visual Studio 2015的错误:

1>------ Build started: Project: a2_warn1700, Configuration: Debug Win32 ------
1>  LinkedList.cpp
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C2143: syntax error: missing ';' before '<'
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C2238: unexpected token(s) preceding ';'
1>  Generating Code...
1>  Compiling...
1>  CustomShape.cpp
1>  Generating Code...
1>  Compiling...
1>  main.cpp
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C2143: syntax error: missing ';' before '<'
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C2238: unexpected token(s) preceding ';'
1>  World.cpp
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C2143: syntax error: missing ';' before '<'
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(14): error C2238: unexpected token(s) preceding ';'
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\world.cpp(17): error C2039: 'shapeComponents': is not a member of 'CustomShape'
1>  c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(9): note: see declaration of 'CustomShape'
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\world.cpp(17): error C2228: left of '.isNext' must have class/struct/union
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\world.cpp(18): error C2039: 'shapeComponents': is not a member of 'CustomShape'
1>  c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\customshape.h(9): note: see declaration of 'CustomShape'
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\world.cpp(18): error C2228: left of '.next' must have class/struct/union
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\world.cpp(19): error C3536: 'shape': cannot be used before it is initialized
1>c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\world.cpp(19): error C2227: left of '->draw' must point to class/struct/union/generic type
1>  c:\users\user\documents\laurier\fall 2016\cp411\a2_warn1700\a2_warn1700\world.cpp(19): note: type is 'int'
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

LinkedList.cpp

#include "LinkedList.h"

template <class T> void LinkedList<T>::insert(T* newObject) {
    if (size == 0) {
        size++;
        root = new LinkedList::Node();
        root->obj = newObject;
        lastNode = root;
        currentNode = root;
    }
    else {
        LinkedList::Node *nextNode = root;
        while (nextNode->next != nullptr) {
            nextNode = nextNode->next;
        }
        LinkedList::Node *prevNode = nextNode;
        nextNode->next = new LinkedList::Node();
        nextNode = nextNode->next;
        nextNode->prev = prevNode;
        nextNode->obj = newObject;
        lastNode = nextNode;
    }
}

template <class T> typename T* LinkedList<T>::next() {
    T* object;
    if (currentNode != nullptr) {
        object = currentNode->obj;
        currentNode = currentNode->next;
    }
    return object;
}

template <class T> bool LinkedList<T>::isNext() {
    bool moreVal = false;
    // check if there are more values
    if (currentNode != nullptr) {
        moreVal = true;
    }
    else {
        currentNode = root;
    }
    return moreVal;
}

template <class T> void LinkedList<T>::destroy() {
    Node *nextNode = root;
    while (nextNode->next != nullptr) {
        Node *temp = nextNode;
        nextNode = nextNode->next;
        delete temp;
    }
    delete root;
    delete currentNode;
    delete lastNode;
    size = 0;
    root = new Node();
    currentNode = root;
    lastNode = root;
}

template <class T> typename T* LinkedList<T>::getRoot() {
    T* object = this->root->obj;
    return object;
}

template class LinkedList<Shape>;
template class LinkedList<CustomShape>;

LinkedList.h

#pragma once
#include "Shape.h"
#include "CustomShape.h"

template <class T>
class LinkedList
{
public:
    LinkedList<T>::LinkedList() {};
    LinkedList<T>::~LinkedList() {};
    void insert(T* newObject);
    T* next();
    bool isNext();
    void destroy();
    T* getRoot();
private:
    struct Node {
        T* obj;
        Node* next = nullptr;
        Node* prev = nullptr;
    };
    int size = 0;
    Node *root = nullptr;
    Node *lastNode = nullptr;
    Node *currentNode = nullptr;
};

World.h

#pragma once
#include "LinkedList.h"
#include "Shape.h"
#include "Cube.h"
#include "CustomShape.h"
#include <iostream>

class World
{
public:
    World();
    ~World();
    void drawWorld();
private:
    LinkedList<CustomShape> worldObjects;
};

World.cpp

    #include "World.h"


    World::World() {
        CustomShape* cShape = new CustomShape();
        cShape->insertShape(new Cube);
        worldObjects.insert(cShape);
    }

    World::~World() {
        this->worldObjects.destroy();
    }

    void World::drawWorld() {
        while (worldObjects.isNext()) {
            auto cShape = worldObjects.next();
            while (cShape->shapeComponents.isNext()) {
                auto shape = cShape->shapeComponents.next();
                shape->draw();
            }
        }
    }

CustomShape.h

#pragma once

#include "GL\glut.h"
#include "LinkedList.h"
#include "Point.h"
#include "Shape.h"

class CustomShape
{
public:
    CustomShape();
    ~CustomShape();
    void insertShape(Shape* shape);
    LinkedList<Shape> shapeComponents;
    void drawSelected();
    Point originPoint;
};

提前致谢。

0 个答案:

没有答案