Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. [Solved] QtTest project Linker errors on VS2010
Forum Updated to NodeBB v4.3 + New Features

[Solved] QtTest project Linker errors on VS2010

Scheduled Pinned Locked Moved Installation and Deployment
22 Posts 3 Posters 10.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Link0
    wrote on last edited by
    #10

    They compile and function just fine, but i'll post them.

    Gate.h
    @#ifndef GATE_H
    #define GATE_H

    #include "Element.h"
    #include "Connector.h"
    #include "globals.h"
    #include "Protein.h"

    #include <vector>

    using namespace std;

    class Connector;

    //Gate represents a certain gate-type, which works with proteins.
    class Gate : public Element {
    //Gate Types Available.
    public:
    enum Type {
    AND,
    NOT,
    OR,
    NOR,
    XOR,
    NAND
    };

    protected:
    int status;

    vector<Connector*> input;
    vector<Connector*> output;
    Type type;

    public:
    explicit Gate(enum Type);
    Gate(int);
    Gate(enum Type, Element*);
    Gate(enum Type, Element*, Element*);
    ~Gate();

    void init(enum Type t);

    bool eval();
    void connectInput0(Element*);
    void connectInput1(Element*);
    void connectBoth(Element*, Element*);
    void disconnectInput0();
    void disconnectInput1();
    void disconnectBoth();
    Connector* getInput(int);
    Connector* getOutput(int);

    vector<Connector*> getInputVector(){return input;};
    vector<Connector*> getOutputVector(){return output;};

    Element* getInput0Elem();
    Element* getInput1Elem();

    int getType(){return type;};
    int getId() {return id;};
    virtual int getStatus() {return status;};
    };

    #endif
    @

    Gate.cpp
    @#include "Gate.h"
    #include "globals.h"
    #include "Connector.h"
    #include "Element.h"

    //Create Gate with type alone.
    Gate::Gate(enum Type t) {
    init(t);
    type = t;
    this->evaled = false;
    }
    Gate::Gate(int i) {
    Type t = (Type) i;
    init(t);
    type = t;
    this->evaled = false;
    }

    //Create Gate with type and 1 element to connect to.
    Gate::Gate(enum Type t, Element* l) {
    init(t);
    if(l) {
    connectInput0(l);
    } else {
    throw error();
    }
    type = t;
    this->evaled = this->eval();
    }

    //Create Gate with type and 2 elements to connect to.
    Gate::Gate(enum Type t, Element* l, Element* r) {
    //Not gate is not allowed to have two connections made.
    if((type = t) == Gate::NOT) {
    throw error();
    }

    init(t);

    //nullptr checks.
    if(l) {
    connectInput0(l);
    } else {
    throw error();
    }

    if(r) {
    connectInput1(r);
    } else {
    throw error();
    }

    //TODO remove debug value.
    this->evaled = this->eval();
    }

    Gate::~Gate() {
    for(int i = 0; i < input.size(); i++) {
    delete input.at(i);
    }

    for(int i = 0; i < output.size(); i++) {
    delete output.at(i);
    }
    }

    // This function is used to evaluate if this gate is true,
    // according to the gate-type and connections.
    bool Gate::eval() {
    // If nothing connected, default to false else call eval on the connection.
    bool l = this->getInput(0)->isConnected() ?
    this->getInput(0)->eval() : false;

    bool r = false;
    if(this->type != Gate::NOT) {
    r = this->getInput(1)->isConnected() ?
    this->getInput(1)->eval() : false;
    }

    bool result = false;

    //eval according to Gate::Type
    switch(this->type) {
    case AND :
    result = (l && r);break;
    case NOT :
    result = !(l);break;
    case OR :
    result = (l || r);break;
    case NOR :
    result = !(l || r);break;
    case XOR :
    result = (!l && r || l && !r);break;
    case NAND :
    result = !(l && r);break;
    default :
    break;
    }

    //set the gate status accordingly, used for GUI representation.
    result ? this->status = 1 : this->status = -1;
    return result;
    }

    // function used to connect p to the left connector.
    // if nullptr, disconnect.
    void Gate::connectInput0(Element *e) {
    if(e == nullptr) {
    this->getInput(0)->disconnect();
    } else {
    this->getInput(0)->connect(e);
    }
    }

    // function used to connect p to the right connector.
    void Gate::connectInput1(Element *e) {
    if(e == nullptr) {
    this->getInput(1)->disconnect();
    } else {
    this->getInput(1)->connect(e);
    }
    }

    // function used to connect both connectors at once.
    // warning, passing any of the Elements as a nullptr
    // will disconnect any existing connection.
    void Gate::connectBoth(Element* e0, Element *e1) {
    connectInput0(e0);
    connectInput1(e1);
    }

    // disconnects left.
    void Gate::disconnectInput0() {
    this->getInput(0)->disconnect();
    }

    // disconnects right.
    void Gate::disconnectInput1() {
    if(this->type != Gate::NOT) {
    this->getInput(1)->disconnect();
    } else {
    throw error();
    }
    }

    // disconnects both.
    void Gate::disconnectBoth() {
    disconnectInput0();
    disconnectInput1();
    }

    Connector* Gate::getInput(int i) {
    if(i == 1) {
    if (this->type == Gate::NOT) {
    throw error();
    }
    }
    return this->input.at(i);
    }
    //returns the Element connected to the left connector.
    Element* Gate::getInput0Elem() {
    if(this->getInput(0)->isConnected()) {
    return this->getInput(0)->traverse();
    }
    //think of throwing error here..
    return nullptr;
    }

    //returns the Element connected to the right connector.
    Element* Gate::getInput1Elem() {
    if(this->type == Gate::NOT) {
    throw error();
    } else {
    if(this->getInput(1)->isConnected()) {
    return this->getInput(1)->traverse();
    }
    //think of throwing error here..
    }
    return nullptr;
    }

    //initilize all needed variables
    void Gate::init(enum Type t) {
    this->id = currElementId++;
    this->evaled = 0;

    if(t == Gate::NOT) {
    this->input = vector<Connector*>(1);
    } else {
    this->input = vector<Connector*>(2);
    }
    this->output = vector<Connector*>(1);

    for(int i = 0; i < input.size(); i++) {
    this->input.at(i) = new Connector(this);
    }

    for(int i = 0; i < output.size(); i++) {
    this->output.at(i) = new Connector(this);
    }
    }@

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #11

      You got 2 constructors with almost the same parameter list. Both parameters are basically int.
      @public:
      explicit Gate(enum Type);
      Gate(int);
      @

      Together with explicit, that could be the source of a problem. However, that is more poking in the fog and I would not do it this way. Therefore, my recommendation would be to simplify and make it homogeneous with either constructor.

      However, this should not be a problem for the destructor. So, it is a bit fuzzy, but may be you should go step by step.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Link0
        wrote on last edited by
        #12

        That was actually a stab in the dark on my end, back when i thought enumerators were to blame. without it the same thing happened.

        Anyway, i've changed the test to a notably less complex class, and the same linker errors still occur.

        new file in question:
        QConnector.h
        @#ifndef CONNECTORITEM_H
        #define CONNECTORITEM_H

        #include "qgraphicsitem.h"

        class QConnector: public QGraphicsItem
        {
        public:
        QConnector(QGraphicsItem*);
        ~QConnector(void);
        void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);
        QRectF boundingRect() const;
        int parX, parY, parW, parH;

        };

        #endif@

        and the test:
        @void GateTest::connTest(){
        QConnector* conn = new QConnector(0);
        QCOMPARE(1,1);
        }@

        That said, which steps do you mean by step by step.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #13

          [quote author="Link0" date="1337076512"]
          That said, which steps do you mean by step by step. [/quote]

          Removing the duality of constructors and seeing what the next issue is.

          If you the same (should be a different now, because you changed classes) linking error, it has something to do with the linking process. Use a small example and pack everything into one source and one header file. This should work. After this separate the files. This may help to localize your problem.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #14

            [quote author="Link0" date="1337068365"]

            Write the original project, including Gate.cpp and Gate.h

            Add a new test project to the original project's solution.

            Added the QtTest module to the test project.

            Add the original project's main folder as a dependency

            Write the toUpper() test and successfully build.

            Write TypeTest(), and get the linker errors.

            [/quote]

            Seems you did something basicly wrong.

            As I said before, if your original project is library, you should link the library to your test project. if not, you should add the source files to your test project.

            "dependency" don't help you to solve the problem. They are still two dependent projects, the only side effect is that when you build your test project, original project will be build first.

            Debao

            1 Reply Last reply
            0
            • L Offline
              L Offline
              Link0
              wrote on last edited by
              #15

              Right you are! Had to add the folder under additional include directories. Apparently no one ever did. Thank you for both of your time.

              (they really need to make VS more straightforward)

              EDIT: wait. dang. i had the relevant code commented in this version. problem persists.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Link0
                wrote on last edited by
                #16

                Some testing later.

                Putting all code into a header file and then including that worked, as soon as i pull the relevant code back into a cpp file, the linker errors return.

                As for the other idea, It's not a library, so i can't link to it statically.
                Attempting to add the .cpp file to the project did not work either, surprisingly, and i'm not entirely sure if i should do that in any case, considering the code changes and such.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #17

                  [quote author="Link0" date="1337068365"]
                  These are the exact steps i've performed in VS2010 to get here:

                  Write the original project, including Gate.cpp and Gate.h

                  Add a new test project to the original project's solution.

                  Added the QtTest module to the test project.

                  Add the original project's main folder as a dependency

                  Write the toUpper() test and successfully build.

                  Write TypeTest(), and get the linker errors.

                  [/quote]

                  What do you actually mean with:

                  Add the original project's main folder as a dependency

                  ??

                  Are you adding the files of your original project as "existing items" ?

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Link0
                    wrote on last edited by
                    #18

                    Visual Studio allows me to define projects as "project dependencies". It also allows me to add them as references, but the point of that is still eluding me as well. (linking errors in that case too)

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #19

                      The project dependency is for projects with libs. You got 3 projects lib1, lib2 and a main. You change main, it will compile main and link the lib1 and lib2 to main to get exe. You change lib2, lib2 will be compiled and linked with lib1 and main to exe. And whatever mutations.

                      However, you have libs and the outcome, the libs, has to be linked to your project. To my understanding you have no libs, but you got two exe in two projects. For this it does not work. You need to add the source and header files as "existing items" to your test applications. They will be compiled again and linked to your test application.

                      Otherwise you can create a lib project and two application projects. However, you need to link to both application projects the lib. Otherwise it will not work.

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        Link0
                        wrote on last edited by
                        #20

                        Yeah, I've been trying to split the code into a library for the last hour or so. Getting some rather profound linker errors on a specific class so I'll need to look into that.

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          Link0
                          wrote on last edited by
                          #21

                          There, fixed them. Some weird or nonstandard includes throwing a fit. The tests now work as they should, and there's no more linker errors. This time for real, i double checked.

                          Thank you again for your support, both of you.

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            koahnig
                            wrote on last edited by
                            #22

                            Good to know that your problem has been solved.

                            I marked your post as [Solved]. Please do next time.

                            Vote the answer(s) that helped you to solve your issue(s)

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved