Here is what I have so far:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <istream>
#include <string>
#include <vector>
using namespace std;

class dm_energy_vec
{
public:
	vector<float> m_energy_dep;
};

int main()
{
	const int phantom_size = 50; 
	int energy_index;
	vector<float> xpos;
	vector<float> energy;
	vector<float> energy_dep (0, phantom_size);
	vector<dm_energy_vec> energy_dep_sum;

// Create two vectors: xpos and energy. 
	for(int i = 0; i < 25; i++)
		xpos.push_back(0.325*i); 

	for(int i = 0; i < 25; i++)
		energy.push_back(5.364*i); 

//create another vector, energy_index that is the ceiling value of the xpos values
	for(int i = 0; i < 25; i++)
	{
		energy_index = ceil(xpos.at(i));
		//use the energy_index values as the index for another vector, energy_dep, which stores the energy values
		energy_dep.at(energy_index) = energy.at(i);

	//store each energy_dep vector in energy_dep_sum vector which is defined by a class
		energy_dep_sum.push_back(energy_dep);

	}

};

This program first creates two vectors: xpos and energy. xpos and energy contain floating point values. Next, the energy_index vector is created which contains the ceiling values of the xpos vector. Next, the vector, energy_dep, is created which contains the energy values at index, energy_index. What I am trying to do is to store each energy_dep vector into the energy_dep_sum vector using the class dm_energy_vec. When I try to compile this program there is an error on line 40 which reads the following:

Error 4 error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'dm_energy_vec &&'

Does anybody know what the problem is with my code?

Recommended Answers

All 4 Replies

At line fourty you have a type mismatch.

Make your class as such:

class dm_energy_vec
{
public:
        dm_energy_vec(){}
        dm_energy_vec(const vector<float>& vec): m_energy_dep(vec){}
	vector<float> m_energy_dep;
};

and in line 40, change to:

energy_dep_sum.push_back( dm_energy_vec(energy_dep));

but honestly see no point for that class.

I think that your intent with the dm_energy_vec class was to just make an alias for the vector<float> type, this can be done with a typedef, as follows:

typedef vector<float> dm_energy_vec;

Then, all your code should work.

If you actually want to make the dm_energy_vec class more complicated (not just an alias for the vector of floats), then use firstPerson's solution.

Thanks a lot that solves my problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.