I ran this code on a different IDE and it was successful. For some reason I get the above error message on Xcode. I assume I'm missing a header of some kind, but I'm not sure which one.
#include <iostream>
#include <limits>
#include <string>
#include <vector>
int main() {
vector<string> listRestaurants; // error: Implicit instantiation of undefined template
return 0;
}
Xcode 10.2.1 was showing me the error
Implicit instantiation of undefined template 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >'.
#include <iostream>
#include <vector>
int main(int argc, const char * argv[]) {
std::vector<std::string> listRestaurants;
....
return 0;
}
Fixed my issue.
If adding std:: is not the issue for you, then check if you have #include <vector>. That fixed the issue for me.
Didn't realize that #include <vector> is required. I thought it was part of standard library template; I ran this code in VSCODE IDE and it worked fine for me
#include <iostream>
#include <vector>
using namespace std;
int main()
{
uint_least8_t i; // trying to be conscious of the size of the int
vector<int> vect;
for(i = 0; i < 5; ++i)
{
vect.push_back(i);
}
for(auto i : vect)
{
cout << i << endl;
}
return 0;
}
From the comments:
The std namespace houses both of those templates. Change vector to std::vector, and string to std::string. – WhozCraig
the vector and string were placed in the namespace std
using namespace std;
Related
I'm trying to build a class named "Tombola" which should contain as private variable an empty vector. This should be filled at runtime through the class member Tombola.estrai(), which generates a random number and insert it inside the vector named "order" by the method order.push_back(number). This is the class definition in the tombola.h header:
#ifndef TOMBOLA_H
#define TOMBOLA_H
#include <cstdlib>
#include <vector>
using namespace std;
class Tombola {
private:
bool on_off[90];
int tabellone[9][10];
int x_max = 9;
int y_max = 10;
vector<int> order;
public:
Tombola();
~Tombola();
void nuovo();
int estrai();
bool completato();
void stampa();
void stampa_tab();
};
#endif
And this is the implementation of constructor/destructor and Tombola::estrai() inside tombola.cc:
#include "tombola.h"
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <iostream>
using namespace std;
Tombola::Tombola () {
vector<int> ord;
order = ord;
int z=1;
for(int i=0;i<90;i++) {
on_off[i] = false;
}
for(int j=0;j<=x_max;j++) {
for (int k=0;k<=y_max;k++) {
tabellone[j][k] = z;
z++;
}
}
}
Tombola::~Tombola() {
cout << "Tombola is destroyed" << endl;
}
int Tombola::estrai() {
srand(time(NULL));
int estrazione = int(ceil(rand()/double(RAND_MAX)*90));
on_off[estrazione]==true;
order.push_back(estrazione);
return order.back();
}
and this is the call to the method in the main.cpp file:
#include "tombola.h"
#include <cstdlib>
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main () {
Tombola natale;
cout << natale.estrai();
}
When I compile the program everything goes fine, but when I execute the main I get a segmentation fault error which seems to be due to some sort of allocation error when trying to store the item inside the order vector, as reported by the debugger. Could someone explain to me how to solve the error and why the error occours? Thank you.
The reason of segmentation fault is in the constructor. You have to change for(int j=0;j<=x_max;j++) to for(int j=0;j<x_max;j++) in order not to cross the bounds of the array.
for(int j=0;j<x_max;j++) {
for (int k=0;k<y_max;k++) {
tabellone[j][k] = z;
z++;
}
}
However, there are also some minor issues in the code that are worth being mentioned
declaring default-initialized ord vector and assigning it to order is pointless because order is already default-initialized.(See member initializer list for more information).
using namespace std; in a header file is a terrible idea, because if you had a large codebase, and had multiple source files where you want to include that header, everywhere the using statement will be applied, which probably is not desired.
I am creating a game of Tic Tac Toe. I am trying to initialize a grid, which will be a 2 dimensional vector.
Here is my main code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include "ttt_functions.hpp"
using namespace std;
int main() {
vector< vector<int> > grid = initialize();
for (vector <int> i : grid) {
for (int j : i) {
cout << j << " ";
}
cout << "\n";
}
}
Here is my prototype file (.hpp):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector< vector<int> > initialize();
And here are my functions:
#include <iostream>
#include <vector>
using namespace std;
vector< vector<int> > initialize(){
for (int i = 0; i < 3; i ++) {
std::vector<int> new_vec(0,3);
grid.push_back(new_vec);
}
return grid;
}
I have consistently run into this error:
Undefined symbols for architecture x86_64:
"initialize()", referenced from:
_main in ttt-15c935.o
I have looked at other stack overflows and they said the error is a problem with the prototype not matching the function, but I cut and copied the prototype onto the function so I know they are identical and I am still getting this error. Please advice, that would be greatly appreciated.
Your third file which contains the initalize() function body should include the header (prototype) file with function declaration.
I found the answer: I wasn't compiling with the functions files, so I was getting the error. I compiled instead with both files named , like this:
g++ ttt.cpp ttt_functions.cpp
After writing my header file and trying to use it in the cpp.file. The compiler gives me an error when trying to redefine the function in header file.
I didn't face this problem the previous times I was using headers in a similar way. Maybe I initialize the Vector in a wrong way. Anyways here is the code:
#include <string>
#include <vector>
#include "lajitellut.h"
using namespace std;
namespace otecpp_lajitellut{
/*this is where the error appears*/
vector<string> lajitellut(int lkm, char*mjt[]){
vector<string> stringVector;
for(int i =0; i<lkm; i++){
stringVector.push_back(mjt[i]);
}
for(int i =0; i<lkm; i++){
for(int a = 0; a<lkm;a++){
if(stringVector[i] < stringVector[a]){
stringVector[i].swap(stringVector[a]);
}
}
}
return stringVector;
}
}
And here is the header file
#ifndef kissa
#define kissa
#include <string>
#include <vector>
namespace otecpp_lajitellut{
std::vector <std::string> lajitellut(int lkm, char* mjt[]) {
std::vector<std::string> stringVector;
return stringVector;
}
}
#endif // kissa
Put only the function declaration in the "lajitellut.h" header file:
#include <vector>
#include <string>
namespace otecpp_lajitellut {
std::vector<std::string> lajitellut(int, char*);
}
Put the function definition in the source "*.cpp" file:
#include <iostream>
#include <vector>
#include <string>
#include "lajitellut.h"
namespace otecpp_lajitellut {
std::vector<std::string> lajitellut(int lkm, char* mjt[]) {
// your code in here
}
}
int main(){
auto a = otecpp_lajitellut::lajitellut(10, "asd");
}
Note that definition is also a declaration. That being said you don't have a vector there. You have a function of type std::vector<std::string>. Don't use using namespace std;.
Ron is right.
Your function lajitellut() is already implemented in the .h file with the same signature. You can not create a double in the same namespace.
You can change the arguments or the type of the return value or change the namespace in the .cpp file.
For some reason, I need to have a map from arbitrary huge number to double and I tried to implement it with c++98 (and I have to) and Xcode but it doesn't work:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include "gurobi_c++.h"
#include <sstream>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include <cmath>
#include <gmp.h>
using namespace std;
using namespace ::boost::tuples;
using namespace ::boost;
int main()
{
map<mpz_t, double>J;
mpz_t a,b,c,n;
string tempstring;
int xrange=5,yrange=5,component=5;
mpz_set_str(n,"11", 10);
J[n]=-1;
return 0;
}
The error shown is: Array initializer must be an initializer list. Could someone help me with it? Thank you:)
Here's the detail error page:
I don't know the details of mpz_t. However, it appears to be an array.
You can get around the problem by defining a class to be used as the key in your map.
I am able to create an executable using the following code with g++ 4.8.2.
#include <map>
using namespace std;
typedef int (mpz_t)[2];
struct MyKey
{
// Add a proper implementation of a constructor
// with mpz_t.
MyKey(mpz_t in) {}
// Add a proper implementation of copy constructor.
MyKey(MyKey const& copy) {}
// Add a proper implementation of assignment operator.
MyKey& operator=(MyKey const& rhs)
{
return *this;
}
bool operator<(MyKey const& rhs) const
{
// Add a proper implementation.
return false;
}
mpz_t n;
};
int main()
{
map<MyKey, double> J;
mpz_t n;
J[n] = 1.0;
return 0;
}
I'm having a weird problem compiling a function when I try using multiple files. I've boiled it down to this simple example: suppose I want to find the sum of a vector of integers. If I try compiling the following code, it works as expected:
#include <vector>
#include <iostream>
using namespace std;
int VectorSum (const vector<int>& values)
{
int S = 0;
for (int t=0; t < values.size(); t++)
{
S += values[t];
}
return S;
}
int main()
{
vector<int> values;
values.push_back(-100);
values.push_back(75);
values.push_back(75);
cout << "Total = " << VectorSum(values) << endl << endl;
cin.ignore(1, '\n');
return 0;
}
However, if I try using a header file, it crashes on my (error C4430 when compiling on VS 2010 for Windows XP). Here's the code for the other approach:
the header:
/* VectorSum.h */
#pragma once
#include <vector>
int VectorSum (const vector<int>& values);
the source:
/* VectorSum.cpp */
#include "VectorSum.h"
#include <vector>
int VectorSum (const vector<int>& values)
{
int S = 0;
for (int t=0; t < values.size(); t++)
{
S += values[t];
}
return S;
}
the implementation:
/* Main.cpp */
#include "VectorSum.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> values;
values.push_back(-100);
values.push_back(75);
values.push_back(75);
cout << "Total = " << VectorSum(values) << endl << endl;
cin.ignore(1, '\n');
return 0;
}
As you can see, the code for the function in VectorSum.cpp is identical to the code in my first .cpp file, so the problem must be in the header. Any ideas what I'm doing wrong?
#pragma once
#include <vector>
int VectorSum (const std::vector<int>& values);
^^^^^
See the MSDN page for C4430. It is issued in case when a declaration is missing a type or the type is unknown. In your case vector is an unknown type due to unqualified name lookup rules.
It's an issue with the std namespace.
Change the declaration in the header to:
int VectorSum (const std::vector<int>& values);
And make sure there's either a using namespace std; in the .cpp file(s) (like your first example) or that you use the std namespace appropriately when calling/defining the function. For example, you'd need to do one of these things in your VectorSum.cpp file.
As an aside, please don't add a
using namespace std;
statement to the header file. That will force the namespace to be brought into scope for all users of the header (even if it's indirectly included, so it might not be obvious), which might not fit their wants or needs.
The problem is indeed in the header file. You forgot to add
using namespace std;
to the header and thus the compiler doesn't know what vector means.
Corrected header:
/* VectorSum.h */
#pragma once
#include <vector>
using namespace std; // This was missing
int VectorSum (const vector<int>& values); // Now OK, the compiler knows vector
You have to specify the namespace for the vector in VectorSum.h:
int VectorSum (const std::vector<int>& values);
Add a using namespace std
/* VectorSum.h */
#pragma once
#include <vector>
<--------
//using namespace std;
int VectorSum (const std::vector<int>& values);
Try to avoid using namespace in header files to avoid name conflicts.