Click here to Skip to main content
15,446,336 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: What IDE is good for developing GUI apps using C++? Pin
Code4Ever30-May-22 7:13
MemberCode4Ever30-May-22 7:13 
GeneralRe: What IDE is good for developing GUI apps using C++? Pin
Richard MacCutchan30-May-22 9:07
mveRichard MacCutchan30-May-22 9:07 
GeneralRe: What IDE is good for developing GUI apps using C++? Pin
Greg Utas29-May-22 8:18
mveGreg Utas29-May-22 8:18 
GeneralRe: What IDE is good for developing GUI apps using C++? Pin
Calin Cali29-May-22 8:34
MemberCalin Cali29-May-22 8:34 
AnswerRe: What IDE is good for developing GUI apps using C++? Pin
Joe Woodbury29-May-22 13:44
professionalJoe Woodbury29-May-22 13:44 
AnswerRe: What IDE is good for developing GUI apps using C++? Pin
Richard MacCutchan29-May-22 22:12
mveRichard MacCutchan29-May-22 22:12 
AnswerRe: What IDE is good for developing GUI apps using C++? Pin
Gerry Schmitz30-May-22 6:12
mveGerry Schmitz30-May-22 6:12 
Questionvector and Template confusion Pin
T Bones Jones23-May-22 7:33
MemberT Bones Jones23-May-22 7:33 
I will start with my code:

template < typename T1, typename T2 >
void printvector(T1& fout, const std::vector < T2 >& V)
{   
    if (V.empty()) fout << "EMPTY\n";
    else {
        for (size_t x = 0; x != V.size() - 1; ++x)
        {
           fout << V[x] << ", ";
        }
        fout << V[V.size() - 1] << '\n';
    }
}


fstream StatFile;
    StatFile.open(STATICFILE, std::fstream::out);
    if (StatFile.is_open())
    {
      for (i = 0; i != NewLandmarks.size(); ++i)
            {
               cout << NewLandmarks[i].name << " "; printvector(cout, Subtractions);
               StatFile << NewLandmarks[i].fiducial << ", " << NewLandmarks[i].name << ", "; NewLandmarks[i].location.print(StatFile); 
               if (Subtractions.empty()) StatFile << "\n";
               else {
                   StatFile << ','; printvector(StatFile, Subtractions);
               }
            }
		StatFile.close();
	}


The template is supposed to print out the contents of a vector, either to the console when given cout, or to a text file when given an fstream variable.

NewLandmarks is a vector of a struct and that seems to be working just fine here.

Subtractions is a vector of size_t, and this is where the problem lies.

When the code runs, the NewLandmarks name is printed to the console, as well as the contents of the Subtractions vector, which in this case is always 45, 46, 47, 48

When the code gets to the next line, where it prints to the file, NewLandmarks.fiducial, NewLandmarks.name and NewLandmarks.location.print(StatFile) all work as expected.

However, the Subtractions vector only prints its last element, 48, to the file. This implies that the Subtractions vector somehow got changed by the printvector template. But, I don't see how, or why.

Even more confusing, this code loops several times. The first time Subtractions prints to the file as 48. The second time it prints as 47, 48. The third time as 46, 47, 48. I don't loop through a fourth time, but I think I can see what would happen. But I still don't know why. Even worse, during each of these loops when Subtractions is printed to the console I always get the full vector: 45, 46, 47, 48.

So,

Why is the template function behaving differently from its console and file printing activations?
Why does the template function write only the last element of the vector to the file and its first iteration?
Why does the template function change behavior and write an additional vector element each time it runs?

In case it is relevant, NewLandmarks.location is a Vector (a mathematical Vector class that I created) and its print function looks like this:

(L is the length of the Vector)

void Vector::print(std::fstream & fout) const {
    for (size_t i = 0; i != L; ++i) {
        fout << std::setprecision(15) << std::setw(22) << v[i];
        if (i != L - 1) fout << ',';
    }
}


Even if this is somehow causing my problem, I still do not see how or why. But, it seems to be working fine.


Thank you for your help
AnswerRe: vector and Template confusion Pin
Victor Nijegorodov23-May-22 8:09
MemberVictor Nijegorodov23-May-22 8:09 
GeneralRe: vector and Template confusion Pin
T Bones Jones23-May-22 8:12
MemberT Bones Jones23-May-22 8:12 
AnswerRe: vector and Template confusion Pin
CPallini23-May-22 22:02
mveCPallini23-May-22 22:02 
GeneralRe: vector and Template confusion Pin
T Bones Jones24-May-22 4:24
MemberT Bones Jones24-May-22 4:24 
GeneralRe: vector and Template confusion Pin
CPallini24-May-22 20:02
mveCPallini24-May-22 20:02 
AnswerRe: vector and Template confusion Pin
Richard MacCutchan23-May-22 22:03
mveRichard MacCutchan23-May-22 22:03 
GeneralRe: vector and Template confusion Pin
T Bones Jones24-May-22 4:33
MemberT Bones Jones24-May-22 4:33 
GeneralRe: vector and Template confusion Pin
Richard MacCutchan24-May-22 5:02
mveRichard MacCutchan24-May-22 5:02 
GeneralRe: vector and Template confusion Pin
T Bones Jones24-May-22 5:08
MemberT Bones Jones24-May-22 5:08 
GeneralRe: vector and Template confusion Pin
Richard MacCutchan24-May-22 5:11
mveRichard MacCutchan24-May-22 5:11 
QuestionApplication check for virus Pin
Gopi Nath23-May-22 1:18
MemberGopi Nath23-May-22 1:18 
AnswerRe: Application check for virus Pin
Richard MacCutchan23-May-22 1:25
mveRichard MacCutchan23-May-22 1:25 
AnswerRe: Application check for virus Pin
Dave Kreskowiak23-May-22 2:16
mveDave Kreskowiak23-May-22 2:16 
AnswerRe: Application check for virus Pin
Gopi Nath23-May-22 18:30
MemberGopi Nath23-May-22 18:30 
QuestionSetting Font and Text Colors items of ComboBox Pin
ForNow22-May-22 12:32
MemberForNow22-May-22 12:32 
AnswerRe: Setting Font and Text Colors items of ComboBox Solved almost Pin
ForNow22-May-22 13:28
MemberForNow22-May-22 13:28 
GeneralRe: Setting Font and Text Colors items of ComboBox Solved almost Pin
Victor Nijegorodov22-May-22 20:14
MemberVictor Nijegorodov22-May-22 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.