I want to define a vector with static content in a function that might be used by multiple programs. However I am facing some problems.
The main.cpp looked like this:
#include <iostream>
#include <boost/assign/std/vector.hpp>
using namespace std;
using namespace boost::assign;
int main (int argc, char* argv[]) {
vector< int > v;
v += 3,5,1;
for (int i=0; i<v.size(); i++) {
cout << "v: " << i << " " << v[i] << endl;
}
}
This works and I get as an output:
v: 0 3
v: 1 5
v: 2 1
However if I try to put the vector definition into a separate file it does not work.
main.cpp
#include <iostream>
#include "vector_test.hpp"
using namespace std;
int main (int argc, char* argv[]) {
vector< int > v;
for (int i=0; i<v.size(); i++) {
cout << "v: " << i << " " << v[i] << endl;
}
}
vector_test.hpp:
#include <boost/assign/std/vector.hpp>
#include <stdio.h>
using namespace std;
using namespace boost::assign;
static std::vector<int> v;
v += 3,5,1;
Trying to compile this gives me an error:
'v' does not name a type
and the qt creator also tells me:
expected a declaration
How do I fix this?
One way would be to have a function returning a reference to a static instance:
const std::vector<int>& get_magic_vector()
{
static std::vector<int> v{3, 5, 1};
return v;
}
Then
for (auto i : get_magic_vector())
std::cout << i << " ";
In your other files you must say that the variable exists but is defined in another file. That is, in otherFile.cpp you place the following line at file scope:
extern std::vector<int> v;
Related
I'm trying to use the function norm_2_vector from boost.
But I'm getting the error ‘norm_2_square’ was not declared in this scope.
To compile I used the command below, where testNormSquare is the name of the program:
g++ -o testNorm2Square testNorm2Square.cpp
Question: Is there anything I'm missing? What should I do to make the code compilable?
A toy example that is not working is given below.
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v(i) = i;
std::cout << 2.0 * v << std::endl;
std::cout << v * 2.0 << std::endl;
std::cout << norm_2_square(v);
}
The error message is the following:
testNorm2Square.cpp:12:18: error: ‘norm_2_square’ was not declared in this scope
Another problem happens if I specify explicitly that norm_2_square belongs to boost::numeric::ublas:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v(i) = i;
std::cout << 2.0 * v << std::endl;
std::cout << v * 2.0 << std::endl;
std::cout << boost::numeric::ublas::norm_2_square(v);
}
In this case the error message is:
testNorm2SquareV2.cpp:12:41: error: ‘norm_2_square’ is not a member of ‘boost::numeric::ublas’
The following code inserts only one value to the vector col.
The code is extracted from DBMS code base (for importing files), specifically, it is from 1
The code uses void* to be able to read any field type (int, float, and so on).
#include <iostream>
#include <vector>
using namespace std;
void add(std::vector<void*> &col){
reinterpret_cast<std::vector<int>&>(col).push_back( 1);
reinterpret_cast<std::vector<int>&>(col).push_back( 2);
reinterpret_cast<std::vector<int>&>(col).push_back( 13);
}
int main() {
std::vector<void*> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout <<reinterpret_cast<std::vector<int>&> (col)[i] <<endl;
return 0;
}
I am not sure how this code work?
Your code is exhibiting undefined behavior.
std::vector<void*> and std::vector<int> are two completely separate and unrelated types, you can't safely cast between them the way you are, especially since there is no guarantee that void* and int are the same byte size.
Cast the values you are pushing, don't cast the vector itself, eg:
#include <iostream>
#include <vector>
#include <cstdint>
using namespace std;
void add(std::vector<void*> &col) {
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(1)));
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(2)));
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(13)));
}
int main() {
std::vector<void*> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout << reinterpret_cast<intptr_t>(col[i]) << endl;
return 0;
}
Of course, you really should be using the proper container type to begin with:
#include <iostream>
#include <vector>
using namespace std;
void add(std::vector<int> &col) {
col.push_back(1);
col.push_back(2);
col.push_back(13);
}
int main() {
std::vector<int> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout << col[i] << endl;
return 0;
}
In following code, I think that structure stSameNameButDifferent is local scope definition and so no problem for it. But I got error in run-time.
(error : process crash)
Can you explain what's wrong with that code?
test_function.h
#ifndef TEST_FUNC_H_
#define TEST_FUNC_H_
void test_a();
void test_b();
#endif
main.cpp
#include <iostream>
#include "test_function.h"
using namespace std;
int main(int argc, const char** argv)
{
cout << "testing for struct scope" << endl;
test_a();
test_b();
return 0;
}
test_a.cpp
#include <iostream>
#include <sstream>
#include <cstdint>
#include <list>
#include "test_function.h"
struct stSameNameButDifferent
{
uint32_t nPlayCode;
uint32_t nGameID;
std::string sGameName;
};
void test_a()
{
std::list<stSameNameButDifferent> lstSt;
for(int i=0; i<10; ++i)
{
stSameNameButDifferent st;
st.nPlayCode = i;
st.nGameID = 100+i;
std::ostringstream osBuf;
osBuf << "Game_" << i;
st.sGameName = osBuf.str();
lstSt.push_back(st);
}
for(auto &st : lstSt)
{
std::cout << st.nPlayCode << ", " << st.nGameID << ", " << st.sGameName << std::endl;
}
}
test_b.cpp
#include <iostream>
#include <sstream>
#include <cstdint>
#include <list>
#include "test_function.h"
struct stSameNameButDifferent
{
uint32_t nPlayCode;
uint32_t nGameID;
float fDiscountRate;
std::string sGameName;
};
void test_b()
{
std::list<stSameNameButDifferent> lstSt;
for(int i=0; i<10; ++i)
{
stSameNameButDifferent st;
st.nPlayCode = i;
st.nGameID = 1000+i;
st.fDiscountRate = (float)i/100;
std::ostringstream osBuf;
osBuf << "Game_" << i;
st.sGameName = osBuf.str();
lstSt.push_back(st);
}
for(auto &st : lstSt)
{
std::cout << st.nPlayCode << ", " << st.nGameID << ", " << st.sGameName << std::endl;
}
}
To avoid clashes of the same struct names in multiple translation units, you have to put them in an unnamed namespace like so:
namespace {
struct stSameNameButDifferent {
uint32_t nPlayCode;
uint32_t nGameID;
std::string sGameName;
};
}
This will make stSameNameButDifferent only seen privately in the corresponding translation unit (.cpp file).
Otherwise the linker will resolve the symbol with the first one found, hence the errors you see at runtime.
you have defined stSameNameButDifferent in global scope, so compiler cannot see and analyze both definitions to same struct and it only take the first one it meet and that's why you are getting an error.
You can use two different namespaces for test_a and test_b, so you will not get any error.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> a;
a.push_back("1 1 2 4");
a.push_back("2 3 3 3");
a.push_back("2 2 3 5");
a.push_back("3 3 3 3");
a.push_back("1 2 3 4");
for (int i=0;i<a.size();i++)
for(int j=0;j<a[i].length();j++)
cout<<a[i].at[j];
return 0;
}
Hi,when I run the code above,there is an error as below:
error C2109: subscript requires array or pointer type
Please help me and tell me why,thanks!
at is a function, need to be called with () not []
update
cout<<a[i].at[j];
// ^^^
to
a[i].at(j)
// ^^^^^
To output string, you don't need to cout each char, just do
for (int i=0; i<a.size(); i++)
{
std::cout << a[i] << "\n";
}
std::cout << std::endl;
Or if C++11:
for(auto const & s : a)
{
cout << s << "\n";
}
It is more simpler to use the range-based for statement. For example
for ( const std::string &s : a )
{
for ( char c : s ) std::cout << c;
std::cout << std::endl;
}
Is the first element of arr1 getting added?
cout is giving me an error. What am I doing wrong?
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <cstddef>
using std::size_t;
int main ()
{
vector <int> ivec1; //defines a vector named ivec1 to hold values not yet defined
int arr1 [5] = {10, 20, 30 , 40, 50}; // defines array named arr1 with 5 values
ivec1.push_back (arr1 [0]);
cout << ivec1 << endl;
return 0;
}
The answer is quite simple: The operation you are invoking is simply not defined. The IO stream library is blissfully unaware of C++ standard library containers (besides std::string) and does not know how to print them. You will need to do that yourself.
std::vector<int> v;
for(auto& x : v)
std::cout << v << " "; // print each element
std::cout << std::endl; // and a linebreak
I guess cout is not able to work with vectors. I'd implement something like this (I'm sorry for my C++ I didn't write in C++ since 2006...
#include "stdafx.h"
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <cstddef>
using std::size_t;
void fillVector(int output[], vector<int>& input, int size)
{
for(int i=0;i<size; i++)
{
input.push_back(output[i]);
}
}
void printVector(vector<int>& input)
{
for(int i=0; i<input.size(); i++)
{
cout << input.at(i);
if(i!=input.size()-1)
{
cout << ",";
}
}
cout << endl;
}
int main ()
{
vector <int> ivec1; //defines a vector named ivec1 to hold values not yet defined
int arr1 [5] = {10, 20, 30 , 40, 50}; // defines array named arr1 with 5 values
int sz = sizeof(arr1) / sizeof(int);
fillVector(arr1, ivec1, sz);
printVector(ivec1);
return 0;
}