Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Definiowanie Vectora

Ostatnio zmodyfikowano 2021-01-14 16:17
Autor Wiadomość
slyys72
Temat założony przez niniejszego użytkownika
Definiowanie Vectora
» 2021-01-14 12:42:29
Witam - nowy użytkownik.
Etap nauki - Vectory.
Zadanie : zdefiniować vector do przechowania 20 liczb naturalnych.

Mój kod:

C/C++
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
   
vector < int > tab( 20 );
}
.

Problem:
Kod sie nie kompiluje. Podczas kompilacji otwiera bibliotekę o nazwie "stl_vector.h" i ustawia tam na lini :
": public _Alloc { "

a
w kompliatorze dostaje log i 11 błedów :


Kompilator: Default compiler
Wykonywanie  g++.exe...
g++.exe "D:\Programy C++\Vector.cpp" -o "D:\Programy C++\Vector.exe"   -W -Wall -Weffc++ -Wfloat-equal -Werror  -I"D:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"D:\Dev-Cpp\include\c++\3.4.2\backward"  -I"D:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"D:\Dev-Cpp\include\c++\3.4.2"  -I"D:\Dev-Cpp\include"   -L"D:\Dev-Cpp\lib"
D:/Dev-Cpp/include/c++/3.4.2/bits/allocator.h: In instantiation of `std::allocator<int>':
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:79:   instantiated from `std::_Vector_base<int, std::allocator<int> >::_Vector_impl'

D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:110:   instantiated from `std::_Vector_base<int, std::allocator<int> >'
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:142:   instantiated from `std::vector<int, std::allocator<int> >'
D:\Programy C++\Vector.cpp:9:   instantiated from here
D:/Dev-Cpp/include/c++/3.4.2/bits/allocator.h:81: warning: base class `class __gnu_cxx::new_allocator<int>' has a non-virtual destructor
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h: In instantiation of `std::_Vector_base<int, std::allocator<int> >::_Vector_impl':
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:110:   instantiated from `std::_Vector_base<int, std::allocator<int> >'
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:142:   instantiated from `std::vector<int, std::allocator<int> >'
D:\Programy C++\Vector.cpp:9:   instantiated from here
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:79: warning: base class `class std::allocator<int>' has a non-virtual destructor
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:79: warning: `struct std::_Vector_base<int, std::allocator<int> >::_Vector_impl' has pointer data members
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:79: warning:   but does not override `std::_Vector_base<int, std::allocator<int> >::_Vector_impl(const std::_Vector_base<int, std::allocator<int> >::_Vector_impl&)'
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:79: warning:   or `operator=(const std::_Vector_base<int, std::allocator<int> >::_Vector_impl&)'
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h: In instantiation of `std::vector<int, std::allocator<int> >':
D:\Programy C++\Vector.cpp:9:   instantiated from here
D:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:142: warning: base class `struct std::_Vector_base<int, std::allocator<int> >' has a non-virtual destructor

Wykonanie zakończone

I jeszcze takie zachowanie - jak definicję vectora zapiszę tak:

vector<int> tab():

to się kompiluje i nie ma żadnego błędu .





Może to nie dla mnie nauczyc sie tego języka :( .
P-177961
killjoy
» 2021-01-14 13:15:19
A spróbuj tak:

C/C++
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
   
vector < int > tab = vector < int >( 20 );
}

Niejednokrotnie mi się zdarzało, że kompilator omyłkowo (chociaż nie jestem pewien, czy sposób bez przypisania jest do końca poprawny) brał takie tworzenie vectora, jak u ciebie za deklarację funkcji.
P-177962
DejaVu
» 2021-01-14 13:16:48
D:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include

Tak na dobry początek sugerowałbym Ci zainstalowanie porządnego środowiska do nauki (np. Visual Studio 2019 community lub ewentualnie jakiegoś CodeBlocksa).

Ty posiadasz jakąś mega starą wersję Dev-C++ (gcc-3.4.2 to brzmi fatalnie -> kompilator sprzed ~15 lat?).

Sugeruję też unikać spacji w ścieżkach do projektu. Co prawda współczesne IDE/narzędzia dobrze je obsługują, ale ja pamiętam, że zdarzały się jakieś problemy w przeszłości przy jakichś bardziej specyficznych scenariuszach więc zalecam unikać spacji :)

/edit:
GCC 3.4.2 wydano we wrześniu 2004 roku... https://gcc.gnu.org/releases.html
P-177963
slyys72
Temat założony przez niniejszego użytkownika
» 2021-01-14 16:17:22
Dziękuję za podpowiedź.
Zmiana na Visual Studio 2019 community rozwiązała problem.
P-177964
« 1 »
  Strona 1 z 1