this iterator does not works
items*[T](a: var Vector[T]):var T =
for i in 0..a.size:
yield a[i]
when I writing this
loadedmesh in this.objdata.LoadedMeshes:
where this.objdata.LoadedMeshes is vector, that uses my iterator. compiler writes: : execution of external compiler program 'vccexe.exe / c --platform: amd64 / nologo / Z7 --platform: amd64 / nologo / Z7 / EHsc -DWIN32_LEAN_AND_MEAN /IC:\Users\tbutton\Documents\nim-0.19. 4 \ lib /FoC:\Users\tbutton\nimcache\main_d\main.cpp.obj C: \ Users \ tbutton \ nimcache \ main_d \ main.cpp 'failed with exit code: 2
"C: \ WINDOWS \ system32 \ cmd.exe" / C "" C: \ Program Files (x86) \ Microsoft Visual Studio 14.0 \ VC \ vcvarsall "amd64 && SET"
cl.exe / c / nologo / Z7 / nologo / Z7 / EHsc -DWIN32_LEAN_AND_MEAN /IC:\Users\tbutton \ Documents \ Knim-0.19.4 \ lib / FoC \ obj C: \ Users \ tbutton \ nimcache \ main_d \ main.cpp
main.cpp
C: \ Users \ tbutton \ nimcache \ main_d \ main.cpp (373): error C2440: =: cannot convert "objl :: Mesh" to "objl :: Mesh *"
C: \ Users \ tbutton \ nimcache \ main_d \ main.cpp (373): note: There is no user-defined conversion operator available to perform this conversion, or the operator cannot be called
C: \ Users \ tbutton \ nimcache \ main_d \ main.cpp (398): error C2440: =: cannot convert "unsigned int" to "NI *"
C: \ Users \ tbutton \ nimcache \ main_d \ main.cpp (398): note: For converting from integer type to pointer, reinterpret_cast is required, a C-style cast or a feature-cast
_LIB_PRIVATE N_NIMCALL(void, drawobject_FOZegcGTpgzfmmpAj9byILQ)(tyObject_object3d_N9b09cyFZdOd39am12l1wYKXg& this_0) {
nimfr_("drawobject", "objects.nim");
{
objl::Mesh* loadedmesh; //loadedmesh variable
loadedmesh = (objl::Mesh*)0;
{
NI i;
NI colontmp_;
i = (NI)0;
colontmp_ = (NI)0;
nimln_(60, "vector.nim");
colontmp_ = this_0.objdata.LoadedMeshes.size();
nimln_(2107, "system.nim");
NI res = ((NI) 0);
{
nimln_(2108, "system.nim");
while (1) {
NI TM_V45tF8B8NBcxFcjfe7lhBw_7;
if (!(res <= colontmp_)) goto LA4;
nimln_(2109, "system.nim");
i = res;
nimln_(61, "vector.nim");
loadedmesh = this_0.objdata.LoadedMeshes[i]; //line 373 error
nimln_(9, "objects.nim");
glPushMatrix_HSqQ9aRIKEWIjxfeGHyXvMQ();
nimln_(10, "objects.nim");
glBegin_PmWgTJR9aQGhx9bHbaAAjDsg(((NU32) 4));
{
NI* face; //face variable
face = (NI*)0;
{
NI i_2;
NI colontmp__2;
i_2 = (NI)0;
colontmp__2 = (NI)0;
nimln_(60, "vector.nim");
colontmp__2 = (*loadedmesh).Indices.size();
nimln_(2107, "system.nim");
NI res_2 = ((NI) 0);
{
nimln_(2108, "system.nim");
while (1) {
NI TM_V45tF8B8NBcxFcjfe7lhBw_6;
if (!(res_2 <= colontmp__2)) goto LA8;
nimln_(2109, "system.nim");
i_2 = res_2;
nimln_(61, "vector.nim");
face = (*loadedmesh).Indices[i_2];//line 398 error
nimln_(12, "objects.nim");
glVertex3f_PLBFtF9cjp5XqYoL4TAKUtw((*loadedmesh).Vertices[(*face)].Position.X, (*loadedmesh).Vertices[(*face)].Position.Y, (*loadedmesh).Vertices[(*face)].Position.Z);
nimln_(2110, "system.nim");
TM_V45tF8B8NBcxFcjfe7lhBw_6 = addInt(res_2, ((NI) 1));
res_2 = (NI)(TM_V45tF8B8NBcxFcjfe7lhBw_6);
} LA8: ;
}
}
}
nimln_(13, "objects.nim");
glEnd_HSqQ9aRIKEWIjxfeGHyXvMQ_2();
nimln_(14, "objects.nim");
glPopMatrix_HSqQ9aRIKEWIjxfeGHyXvMQ_3();
nimln_(2110, "system.nim");
TM_V45tF8B8NBcxFcjfe7lhBw_7 = addInt(res, ((NI) 1));
res = (NI)(TM_V45tF8B8NBcxFcjfe7lhBw_7);
} LA4: ;
}
}
}
popFrame();
}
if you know c++, I thinking, vector should return pointer to itself. but why it does not works? help me!for i in 0..a.size:
Just a basic remark, and others, who are more experienced with C++, can help you with the rest: .. is inclusive range, here you need to use ..<.
This is how i wrap C++ vectors:
type
CppVector* {.importcpp"std::vector", header: "<vector>", byref.} [T] = object
proc newCppVector*[T](): CppVector[T] {.importcpp: "std::vector<'0>()", header: "<vector>", constructor.}
proc newCppVector*[T](size: int): CppVector[T] {.importcpp: "std::vector<'0>(#)", header: "<vector>", constructor.}
proc len*(v: CppVector): int {.importcpp: "#.size()", header: "<vector>".}
proc add*[T](v: var CppVector[T], elem: T){.importcpp: "#.push_back(#)", header: "<vector>".}
proc `[]`*[T](v: CppVector[T], idx: int): T{.importcpp: "#[#]", header: "<vector>".}
proc `[]`*[T](v: var CppVector[T], idx: int): var T{.importcpp: "#[#]", header: "<vector>".}
Not tested that much but it works for me in Agent Smith
Alternatively you should be able to use C++ via https://github.com/fragcolor-xyz/nimline, example.
I don't know this is the best way to make iterator mitems*[T](a: var Vector[T]):var T works, but this code works. It looks like nim just replace calling proc `[]`*[T](this:var Vector[T]; index: int): var T to v[i]. std::vector[T]::operator[ ](size_type pos) returns reference to a element. But a iterator returns var T is mapped to pointer to T in C++ code. (addr a[i])[] returns var T in Nim and become &(v[i]) in C++ code.
{.emit: """
using namespace std;
""".}
type
Vector* {.importcpp: "std::vector", header: "<vector>".}[T] = object
proc constructVector*[T](size: cuint): Vector[T] {.
importcpp: "vector<'*0>(@)", header: "<vector>", constructor.}
proc constructVector*[T](size: cuint; data:var T): Vector[T] {.
importcpp: "vector<'*0>(@)", header: "<vector>", constructor.}
proc constructVector*[T](vector: Vector[T]): Vector[T] {.
importcpp: "vector<'*0>(@)", header: "<vector>", constructor.}
proc destroyVector*[T](this: var Vector[T]) {.importcpp: "#.~Vector()",
header: "<vector>".}
proc `[]`*[T](this:var Vector[T]; index: int): var T {.importcpp: "#[@]",
header: "<vector>".}
proc size*[T](this: var Vector[T]): var int {.importcpp: "size", header: "<vector>".}
iterator mitems*[T](a: var Vector[T]):var T =
for i in 0..<a.size:
yield (addr a[i])[]
# yield a[i]
type
testObj = object
x: int
y: float
proc main =
var t = testObj(x: 7, y: 7)
var v:Vector[testObj] = constructVector[testObj](2, t)
echo v[0]
for i in v.mitems:
i.x = 123
for i in v.mitems:
echo i.x
main()
sorry, I solved the problem
For the sake of somebody else who will have a similar problem in the future, don't just say that, also write how did you solve it.
i wrote
`[]`*[T](this:var Vector[T]; index: auto): var T {.importcpp: "#[@]",
header: "<vector>".}
and problem was solved