Pregunta

I guess I'm doing something wrong with my includes but I just can't seem to find what it is I am doing wrong. I am using visual studio 10 on a w7 64 bit system.

The compile errors I get:

testd.cpp(9): warning C4018: '<' : signed/unsigned mismatch
testd.cpp(21): warning C4018: '<' : signed/unsigned mismatch
testd.cpp(36): error C2065: 'tmp' : undeclared identifier
testd.cpp(37): error C2065: 'tmp' : undeclared identifier
testd.cpp(37): error C2227: left of '->getTestId' must point to class/struct/union/generic type
type is ''unknown-type''
testd.cpp(40): error C2065: 'tmp' : undeclared identifier
testd.cpp(41): error C2065: 'tmp' : undeclared identifier

So every time I am trying to make a new object (tmp) , he says "undeclared identifier". My guess is that he can't get to the class definition of Test (aldho he doesn't warn me), but I don't get why that is.. I reduced my code to a minimum, so that only the errorfunctions remain. Thanks in advance for your time

test.h: definition of a class Test, with 2 atributes, getters, setters, constructors and a destructor

#pragma once
class Test
{
private:
    int myTestId;
    string myTestNaam;    
public:
    Test();
    Test(string TestNaam,int TestId);
    ~Test();
    int getTestId();
    string getTestNaam();
    void setTestId(int TestId);
};

*test.cpp: definition of the functions for class Test

#include "stdafx.h"
#include "headers/Test.h"
Test::Test() {setTestId(0);}
Test::Test(string TestNaam, int TestId) {setTestId(TestId);}
Test::~Test() {}    
void Test::setTestId(int TestId) {myTestId=TestId;}
int Test::getTestId() {return myTestId;}

testd.h: definition of a class Testd, with static vector, and static functions

#pragma once
#include "Test.h"
class TestD
{
private:
    static vector<Test*> Testen;
    static int getPosition(int ID);
public:
    static Test* newTest(Test*);
    static Test* getTest(int ID);
};

testd.cpp: definition of the functions for class Testd

#include "StdAfx.h"
#include "headers/Test.h"
#include "headers/Testd.h"
vector<Test*> TestD::Testen = vector<Test*>();
Test* TestD::getTest(int ID)
{for(int i =0; i < Testen.size(); i++)
    {Test* tmp = Testen.at(i);
    if(tmp->getTestId() == ID)
    return tmp;
}
return 0;
}

int TestD::getPosition(int ID)
{for(int i = 0; i < Testen.size(); i++)
    {Test* tmp = Testen.at(i);
    if(tmp->getTestId() == ID)
    return i;
}
return -1;
}

Test* TestD::newTest(Test* Test)
{if(Test->getTestId()==-1)
    {Test* tmp = Testen.at(Testen.size()-1);
    int newId = tmp->getTestId()+1;
    Test->setTestId(newId);
    }
    Test* tmp = getTest(Test->getTestId());
    if(tmp==0)
{Testen.push_back(Test);
    return Test;
}
}

Finally i include my Stdafx.h

#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
//some includes are not used in the files I mentioned here, but other files need them

Thanks for your help and i hope I didn't offend you by my poor English.

¿Fue útil?

Solución

To start with, you can't define a variable and name it the same way as an existing type:

Test* TestD::newTest(Test* Test)

Rename that Test variable.

Then the signed-unsigned missmatch warning is caused because you are comparing the return value of std::vector::size, which a size_t, with an int:

for(int i =0; i < Testen.size(); i++)

Modify that i's type to size_t and you'll remove that warning:

for(size_t i =0; i < Testen.size(); i++)

Otros consejos

usually when declaring pointer you have to allocate with new, like

Test *temp=new Test(...);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top