0

I am having some trouble with templates. for some reason my template itself seems to be fine but when I try to use it for some reason it errors out even though as far as I know I am using it correctly. The template is for a simple linked list that is meant to hold a child Shape classes and a CustomShape class. This code is for school so if you could only comment on the errors related to the template that would be great. I have read other posts on this subject but none seem to help/solve whatever my issue is.

Errors using 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;
};

Thanks in advance.

matt2405
  • 171
  • 2
  • 13
  • Before anything else, I'd start with this question/answers: [Why can templates only be implemented in header files](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – WhozCraig Oct 12 '16 at 16:39
  • adding the template line for each function and their types to the end of the .cpp linkedlist file does nothing and has the same errors as before... – matt2405 Oct 12 '16 at 16:58
  • You said, " it errors out even though as far as I know I am using it correctly. " - obviously that isn't the case in some form. When asking questions about specific errors, **always** include the exact, *verbatim* error text you're receiving *as part of your question*. The link I provided (and Nathan apparently used for closing this post) is a problem you *will* run into and *should* fix. I made no claims it is the "answer" to your problem. Obviously this: `while (cShape->shapeComponents.isNext)` is wrong. `isNext` is a *member function*, not a member variable. You're missing `()`. – WhozCraig Oct 12 '16 at 17:10
  • you're right. I edited the question as I think I am following the other post answer correctly but it is still not working. The errors that I am getting is just simple syntax errors on declaring a new LinkedList and using the drawWorld. – matt2405 Oct 12 '16 at 17:16
  • And again, put the **exact** error messages you're receiving, completely and verbatim, **in your question**. That should *always* be done when asking questions on Stack Overflow about about error messages. It is *highly* likely an answer will not be forthcoming regardless, as you've not provided a [minimal, complete, verifiable example](https://stackoverflow.com/help/mcve) that reproduces your problem, however. So prepare for that as well. – WhozCraig Oct 12 '16 at 17:19
  • I put the error code, file and line which I think satisfies your complaint. hopefully this can help solve the problem. thanks – matt2405 Oct 12 '16 at 17:29
  • Those are *not* the *exact* error messages *verbatim*. After announcing the line numbers the *reason* for the error at that line is given. Btw, neither `Shape.h` nor `CustomShape.h` should be included in `LinkedList.h` You have a circular reference. that will ultimately break compilation. – WhozCraig Oct 12 '16 at 17:36
  • This looks like part of the error-list in visual-studio. You need to change the tab to the compiler-output tab to see the full messages. – tkausl Oct 12 '16 at 17:40
  • The update is the output from the compiler in VS2015. hopefully that has everything that is needed. As for not including the shape and customshape that works well but then it generates a bunch of linker errors. – matt2405 Oct 12 '16 at 17:43
  • k so i got it to work. getting rid of the includes and using the solution from the duplicate it now works great. thanks for the help and moving forward I will make sure to include exact errors. – matt2405 Oct 12 '16 at 17:47

0 Answers0