• 15 апреля стартует «Курс «SQL-injection Master» ©» от команды The Codeby

    За 3 месяца вы пройдете путь от начальных навыков работы с SQL-запросами к базам данных до продвинутых техник. Научитесь находить уязвимости связанные с базами данных, и внедрять произвольный SQL-код в уязвимые приложения.

    На последнюю неделю приходится экзамен, где нужно будет показать свои навыки, взломав ряд уязвимых учебных сайтов, и добыть флаги. Успешно сдавшие экзамен получат сертификат.

    Запись на курс до 25 апреля. Получить промодоступ ...

Как собрать вектор указателей на класс ?

  • Автор темы Monarh
  • Дата начала
M

Monarh

Здравствуйте.
Подскажите пожалуйста как собрать вектор указателей на класс ?
Что делается в программе:
Код:
// Имеется класс
class Cls_Unit_of_Memory{ ... };

// Имеется вектор указателей на объекты этого класса
vector<Cls_Unit_of_Memory*>	VecCls_Memory;

// Создаём Объект Класса
Cls_Unit_of_Memory* ClsPtr_Unit_of_Memory = NULL;
ClsPtr_Unit_of_Memory = new Cls_Unit_of_Memory;
if( ClsPtr_Unit_of_Memory == NULL ) Message_Error( ... );

// Вставляем Указатель, на только что созданный Объект, в Вектор Указателей
VecCls_Memory.insert( Place_for_Insert( VPtr_Array ), ClsPtr_Unit_of_Memory );
как вариант пробовал
VecCls_Memory.insert( Place_for_Insert( VPtr_Array ), &(*ClsPtr_Unit_of_Memory) );

функция VecItr Place_for_Insert( void* );
находит, в векторе, место для вставки и возвращает итератор на элемент 
перед которым необходимо вставить входной элемент.
Компилятор ругается:
Код:
CLEAN SUCCESSFUL (total time: 23s)

mkdir -p build/Debug/Cygwin-Windows/Source
rm -f build/Debug/Cygwin-Windows/Source/Memory_Allocation.o.d
g++	-c -g -MMD -MP -MF build/Debug/Cygwin-Windows/Source/Memory_Allocation.o.d -o build/Debug/Cygwin-Windows/Source/Memory_Allocation.o Source/Memory_Allocation.cpp
Source/Memory_Allocation.cpp: In copy constructor `Cls_Memory::Cls_Memory(const Cls_Memory&)':
Source/Memory_Allocation.cpp:71: error: no matching function for call to `std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> >::insert(VecItr, Cls_Unit_of_Memory*&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:92: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:612: note: void std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
Source/Memory_Allocation.cpp: In member function `void* Cls_Memory::Allocation(const Max_UInt_Type&, const char*)':
Source/Memory_Allocation.cpp:137: error: no matching function for call to `std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> >::insert(VecItr, Cls_Unit_of_Memory*&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:92: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:612: note: void std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
Source/Memory_Allocation.cpp: In member function `VecItr Cls_Memory::Iterator_of_Array(void*)':
Source/Memory_Allocation.cpp:194: error: conversion from `__gnu_cxx::__normal_iterator<Cls_Unit_of_Memory**, std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> > >' to non-scalar type `VecItr' requested
Source/Memory_Allocation.cpp: In member function `VecItr Cls_Memory::Place_for_Insert(void*)':
Source/Memory_Allocation.cpp:211: error: conversion from `__gnu_cxx::__normal_iterator<Cls_Unit_of_Memory**, std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> > >' to non-scalar type `VecItr' requested
Source/Memory_Allocation.cpp:218: error: conversion from `__gnu_cxx::__normal_iterator<Cls_Unit_of_Memory**, std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> > >' to non-scalar type `VecItr' requested
Source/Memory_Allocation.cpp: At global scope:
Source/Memory_Allocation.cpp:253: error: prototype for `void Cls_Memory::Copy_All_Units_of_Memory_from_Vector_Units_of_Memory(const std::vector<Cls_Unit_of_Memory, std::allocator<Cls_Unit_of_Memory> >&)' does not match any in class `Cls_Memory'
Source/../Header/Memory_Allocation.h:126: error: candidate is: void Cls_Memory::Copy_All_Units_of_Memory_from_Vector_Units_of_Memory(const std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> >&)
Source/Memory_Allocation.cpp: In member function `void Cls_Memory::Copy_All_Units_of_Memory_from_Vector_Units_of_Memory(const std::vector<Cls_Unit_of_Memory, std::allocator<Cls_Unit_of_Memory> >&)':
Source/Memory_Allocation.cpp:275: error: no match for 'operator*' in '*(+VecCls_Vector_Units_of_Memory)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = Cls_Unit_of_Memory, _Alloc = std::allocator<Cls_Unit_of_Memory>](((unsigned int)MaxUInt_Number_of_Unit_of_Memory))'
Source/Memory_Allocation.cpp:279: error: no matching function for call to `std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> >::insert(VecItr, Cls_Unit_of_Memory*&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:92: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:612: note: void std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
Source/Memory_Allocation.cpp: In member function `void Cls_Memory::DeAllocation(void*)':
Source/Memory_Allocation.cpp:394: error: base operand of `->' has non-pointer type `Cls_Unit_of_Memory'
Source/Memory_Allocation.cpp:401: error: base operand of `->' has non-pointer type `Cls_Unit_of_Memory'
Source/Memory_Allocation.cpp:403: error: no matching function for call to `std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> >::erase(VecItr&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:108: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:120: note: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
make: *** [build/Debug/Cygwin-Windows/Source/Memory_Allocation.o] Error 1
BUILD FAILED (exit value 2, total time: 2s)

С вектором объектов все функции проверялись, работают, но память перевыделяется под все объекты при вставке нового,
что в данном случае НЕдопустимо, поэтому необходимо переделать под вектор указателей.

Заранее Всем огромное СПАСИБО.
 
M

Monarh

Вот же ведь как бывает, и вправду дело в итераторе.
Kmet, Вы прямо как в воду глядели :)
Забыл поменять тип итератора
с typedef vector<Cls_Unit_of_Memory>::iterator VecItr;
на typedef vector<Cls_Unit_of_Memory*>::iterator VecItr;
Теперь всё заработало :)
А уже думал придётся писать альтернативу вектору.

Огромное-Преогромное СПАСИБО, Kmet !!!
 

Kmet

Well-known member
25.05.2006
904
8
BIT
0
зачем в воду?! в логи=)

Source/Memory_Allocation.cpp:71: error: no matching function for call to `std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> >::insert(VecItr, Cls_Unit_of_Memory*&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:92: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]
 
M

Monarh

зачем в воду?! в логи=)

Source/Memory_Allocation.cpp:71: error: no matching function for call to `std::vector<Cls_Unit_of_Memory*, std::allocator<Cls_Unit_of_Memory*> >::insert(VecItr, Cls_Unit_of_Memory*&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:92: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Cls_Unit_of_Memory*, _Alloc = std::allocator<Cls_Unit_of_Memory*>]

Я из этого ничего не понял :ph34r:
вроде везде написано про вектор с параметрами Cls_Unit_of_Memory* !?
 

Kmet

Well-known member
25.05.2006
904
8
BIT
0
написано что не может найти insert(VecItr, Cls_Unit_of_Memory*&)', а есто только insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&)
 
M

Monarh

Теперь всё понял :ph34r:
Ещё раз Огромное-Преогромное СПАСИБО !!!
 
Мы в соцсетях:

Обучение наступательной кибербезопасности в игровой форме. Начать игру!