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?

Dani AI

Generated

The compiler error is a type mismatch: a std::vector<dm_energy_vec> cannot accept a std::vector<float> directly. flagged the mismatch, offered a converting constructor, and suggested an alias. Two additional, practical issues were not called out earlier and will cause runtime problems unless fixed: the per-slot vector is constructed with zero length in the posted code, and the ceil → integer conversion must be validated before using it as an index.

Make sure the per-slot container actually has the size you intend and guard the index before assignment. Example pattern:

std::vector<float> energy_dep(phantom_size, 0.0f);          // allocate phantom_size slots
size_t idx = static_cast<size_t>(std::ceil(xpos[i]));      // cast safely to an unsigned index
if (idx < energy_dep.size()) {
    energy_dep[idx] = energy[i];
} else {
    // handle out-of-range index
}

Decide whether you want an alias or a wrapper. If the class is only a synonym for vector<float>, use a type alias to avoid needless copying and conversions. If you keep a wrapper for semantics, add a converting constructor and support move semantics so large vectors are not copied repeatedly. Under C++11+, prefer in-place construction or moves to reduce copies; for example, default-construct a wrapper in the container and then move the filled vector into it to avoid a temporary copy.

Other practical tips: use reserve on energy_dep_sum if you know the final count, clear or assign energy_dep between iterations if reusing it, prefer size_t for indices, and compile with warnings (-Wall -Wextra or MSVC equivalent) to catch these issues early.

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.