[c++] STL problem when trying to load a .obj file

Started by
3 comments, last by bijan311 11 years, 8 months ago
I'm trying to load an obj file using and I have no idea why I'm getting a bunch of errors. Here is the relevant code.
[source lang="cpp"]
//The includes which are not in the same file as the rest of the code
#include <fstream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include "BimanEngine.h"
#include "ModelObject.h"//<- has the VertexStruct struct.

//Read the object file to get the vertices, texture coordinates, indices, and texture indices
vector<D3DXVECTOR3> vertices;
vector<D3DXVECTOR2> texCoords;
vector<VertexStruct> vertexStructs;
set<VertexStruct> vertexStructSet;
map<VertexStruct, int> indexMap;
vector<int> indices;
string input;
ifstream file("Res//texture.obj");
//Check to see if the file could be opened
if(!file.is_open())
g_pGame->Error(TEXT("Could not open Res//untitled.obj"));
UINT timer = GetTickCount();
//Read the file
while(input != "e")
{
file>>input;
//If it's a vertex put it in the vertices
if(input == "v")
{
D3DXVECTOR3 vertex;
file>>vertex.x>>vertex.y>>vertex.z;
vertices.push_back(vertex);
}
//If there's a texture coordinate put it in the texCoords vector
if(input == "vt")
{
D3DXVECTOR2 texCoord;
file>>texCoord.x>>texCoord.y;
texCoords.push_back(texCoord);
}
//If it's a position index, put it in the indices vector and the textureIndices vector (there are 3 indices per "f")
else if(input == "f")
{
for(int i = 0; i < 3; i++)
{
//Get the position index
int pos, texture;
file>>pos;
//Get the texture index, if there is one
if(file.peek() == '/')
{
//skip the "/"
file.ignore();
file>>texture;
}
//Create the vertex structs based on the position and texture indices, if it was created, don't put it in the vector
VertexStruct vs;
vs.Pos = vertices[pos-1];//Subract by one because the indices start at zero, not 1
vs.Tex = texCoords[texture-1];
vs.Color = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
//Find out if the vertex struct was already created
auto it = vertexStructSet.find(vs);
//If it was NOT created, put it in the set and vector and update the index data
if(it == vertexStructSet.end())
{
vertexStructSet.insert(vs);
vertexStructs.push_back(vs);
int index = vertexStructs.size()-1;
indices.push_back(index);
indexMap[vs] = index;
}
//If it WAS created, just update the index data
else
indices.push_back(indexMap[vs]);
}
}
}
file.close();
[/source]

Here are the errors


------ Build started: Project: Direct3D Model Load, Configuration: Release Win32 ------
Build started 8/26/2012 1:24:40 PM.
InitializeBuildStatus:
Touching "Release\Direct3D Model Load.unsuccessfulbuild".
ClCompile:
ModelLoad.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(1885) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
with
[
_Ty=VertexStruct
]
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\set(49) : see reference to class template instantiation 'std::less<_Ty>' being compiled
with
[
_Ty=VertexStruct
]
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(451) : see reference to class template instantiation 'std::_Tset_traits<_Kty,_Pr,_Alloc,_Mfl>' being compiled
with
[
_Kty=VertexStruct,
_Pr=std::less<VertexStruct>,
_Alloc=std::allocator<VertexStruct>,
_Mfl=false
]
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(520) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
with
[
_Traits=std::_Tset_traits<VertexStruct,std::less<VertexStruct>,std::allocator<VertexStruct>,false>
]
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(659) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
with
[
_Traits=std::_Tset_traits<VertexStruct,std::less<VertexStruct>,std::allocator<VertexStruct>,false>
]
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\set(58) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
with
[
_Traits=std::_Tset_traits<VertexStruct,std::less<VertexStruct>,std::allocator<VertexStruct>,false>
]
ModelLoad.cpp(49) : see reference to class template instantiation 'std::set<_Kty>' being compiled
with
[
_Kty=VertexStruct
]
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\string(243) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\string(233) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\string(223) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(1502) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory(2582) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility(1356) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility(1179) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const VertexStruct'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(318) : see declaration of 'std::operator <'
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2676: binary '<' : 'const VertexStruct' does not define this operator or a conversion to a type acceptable to the predefined operator
Build FAILED.
Time Elapsed 00:00:01.80
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thank you in advance
Advertisement
An std::set organizes its content by comparing the elements by the less-than comparison operator (or a separate predicate function) but it seems that you have not provided such an operator for your vertex class (or instantiated the set class with such a predicate). The set simply doesn't know how to compare the elements you put into it. For it to work, and I'm not commenting on the suitability of std::set to store floating point triplets in a set in the first place, you need to define operator < for your vertex class to provide a sorting criteria.

An std::set organizes its content by comparing the elements by the less-than comparison operator (or a separate predicate function) but it seems that you have not provided such an operator for your vertex class (or instantiated the set class with such a predicate). The set simply doesn't know how to compare the elements you put into it. For it to work, and I'm not commenting on the suitability of std::set to store floating point triplets in a set in the first place, you need to define operator < for your vertex class to provide a sorting criteria.


I created a less-than operator like you said, but I still am getting errors. Here is what I added


struct VertexStruct
{
D3DXVECTOR3 Pos;
D3DXCOLOR Color;
D3DXVECTOR2 Tex;
bool operator<(const VertexStruct vs);
bool operator==(const VertexStruct vs);
};

bool VertexStruct::operator<(const VertexStruct vs)
{
if(Pos.x < vs.Pos.x)
return true;
else
return false;
}
If you make the comparison operator a member function, you need to make it const. bool operator<(const VertexStruct vs) const; Though it usually makes more sense to define it as a non-member function.
That solved it; thank you very much.

This topic is closed to new replies.

Advertisement