LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2018, 01:28 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,916

Rep: Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396
C++ 'namefile::mylist' should be initialized in the member initialization list


As petty as this maybe to some, even a bit to me.

In stead of me hacking away at stuff putting this together as I have been. I'm starting anew with this file list cpp I have after discovering more newer functionality I've added to it, and to make this more portable for me as I am always using it in an project, then adding to it, and now its too split up in what this file uses to call in order to add files to it.

I am wanting to make it self containing and as little as possible so I do not have to keep modifying it to make it work in a different project. So I made this struct to contain what I need so far.


my struct
Code:
typedef struct
{
    char *file = nullptr;
    unsigned long int leftoff = 0;
    int set_randomize = -1;
    int get_file_number = -1;

    std::vector<std::string> mylist;

} namefile;
the file is down to this, which I am sure I'll be modifying it removing and or adding what functions I need.

if you where to look closely you'd see different structs being used in different places in order to get the full functionality out of it. Now I am wanting to consolidate it into just this file, with its header for portability.
Code:
/*
 * files.cpp
 * Copyright (C) Michael Heras 2018
 *
  	This is free software: it is for use with ImLib2.
  	you can redistribute it and/or modify it
        under the terms of the GNU General Public License as published by the
        Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.

        It is distributed in the hope that it will be useful, but
        WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
        See the GNU General Public License for more details.

        You should have received a copy of the GNU General Public License along
        with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <random>
#include <dirent.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctime>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h> //type dir/file
#include <stdlib.h> //exit
#include </usr/include/Imlib2.h> //Imlib_Load_Error

#include "files.h"


using namespace boost::filesystem;

struct recursive_directory_range
{
    typedef recursive_directory_iterator iterator;
    recursive_directory_range(path p) : p_(p) {}

    iterator begin() { return recursive_directory_iterator(p_); }
    iterator end() { return recursive_directory_iterator(); }

    path p_;
};
size_t loadFiles(std::vector<std::string> &mylist , boost::filesystem::path p)
{

  for (auto it : recursive_directory_range(p))
  {
      //convert char to string
      std::stringstream newStringPath;
      newStringPath << it;
      std::string myStringPath = newStringPath.str();

    if (  checkFileExt(getFileExt(myStringPath)) == 1 )
    {
         // Remove all double-quote characters
        myStringPath.erase(
        remove( myStringPath.begin(), myStringPath.end(), '\"' ),
        myStringPath.end() );

        mylist.push_back( myStringPath );
    }
 }
 return 0;
 }
std::string getFileExt(const std::string& s)
{
    size_t i = s.rfind('.', s.length());
   if (i != std::string::npos)
   {
      std::string lext = (s.substr(i+1, s.length() - i));
      //remove double quotes off right side
      return (lext.erase(lext.size() - 1));
   }
   return("");
}
size_t checkFileExt(std::string extension)
{
	unsigned int count = 0;
	std::vector <std::string> extensions = {"jpg", "JPG", "png", "bmp", "jpeg" };

	while ( count < extensions.size() )
	{
		if (extension != extensions[count] )
		{
			count++;
			continue;
		}
		else
			return 1;
	}
	return 0;
}

void printList(std::vector<std::string> &mylist)
{
    for (auto i = mylist.begin(); i != mylist.end(); i++)
        std::cout<< *i << std::endl;
}

/*
std::string get_random_file()
{
	int min=0, max=opts.dfile.size() - 1;

	return ( fn.file.at( ( getRany(min,max) ) ) );
}


std::string walk_down()
{
	for (int i = 0; i < 1; i++)
	{
		if (fn.leftoff == opts.dfile.size() - 1)
		{
			opts.leftoff = 0;
			return ( opts.dfile.at(opts.leftoff) );
		}
		else
		{
			opts.leftoff++;
			return ( opts.dfile.at(opts.leftoff) );
		}
	}
	return ( opts.dfile.at(opts.leftoff) );
}


void sort_V()
{
	std::sort(opts.dfile.begin(), opts.dfile.end(), compareNoCase);
}
// Comparison; not case sensitive.
bool compareNoCase (std::string first, std::string second)
{
  unsigned long int  i = 0;
  while ((i < first.length()) && (i < second.length()))
  {
    if (tolower (first[i]) < tolower (second[i])) return true;
    else if (tolower (first[i]) > tolower (second[i])) return false;
    i++;
  }

  if (first.length() < second.length()) return true;
  else return false;
}

void load_image_err(void)
{
	Imlib_Load_Error err;
	std::string newFilename = get_file_name();
	const char* filename = newFilename.c_str();

	if(filename == NULL)
	{
		std::cout<<"File null"<<std::endl;
		exit(1);
}

  imgdata.image = imlib_load_image_with_error_return(filename, &err);
 // print loaded filename to stdout
 if(img.printlistintake == 3)
    std::cout<<filename<<std::endl;

	if (err)
	{

	//spit out image path and name
	std::cout<< "Bad Image: "<<filename<<std::endl;
	  // get next image
	   load_image_err();


	}
}

std::string get_file_name(void)
{
	if( fn.set_randomize == 1)
    {
		return ( get_random_file() );
    }
    else if(fn.get_file_number == 1 )
    {
		if ( img.file_number >  opts.dfile.size() )
		{
			std::cout<<"Image number "<<img.file_number<<" out of range."<<std::endl;
			exit(1);
		}
		else
			return ( opts.dfile.at(img.file_number) );

    }
    else
    {
		return (walk_down());
    }
}

size_t getRany(int min, int max)
{
	std::random_device seed;
	std::mt19937 gen(seed());
    std::uniform_int_distribution<int> dist(min, max);
	return ( dist(gen) );
}
*/
What I'd like to do is get as many warnings out of it as possible, errors of course have to be removed in order for it to work, that's a no brainier. Luckily I have o errors yet. put a pin in that one.

the warning that is bothering me that I've tried looking into, even though this works so far, gets the files and prints them out properly.

it is warning me this,
Code:
||=== Build: Debug in wmslideshow (compiler: GNU GCC Compiler) ===|
/media/data/C++Projects/wmslideshow/files.h||In constructor 'namefile::namefile()':|
/media/data/C++Projects/wmslideshow/files.h|9|warning: 'namefile::mylist' should be initialized in the member initialization list [-Weffc++]|
/media/data/C++Projects/wmslideshow/main.cpp|6|note: synthesized method 'namefile::namefile()' first required here |
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|
But how to initialize this more than I already have, and with the
std::vector<std::string> mylist;
is an unknown size needed, it grows dynamically does it not?

Last edited by BW-userx; 03-31-2018 at 01:48 PM.
 
Old 03-31-2018, 08:50 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
I think that warning can be ignored. From here:https://gcc.gnu.org/onlinedocs/gcc-7...meric-literals
Quote:
When selecting this option, be aware that the standard library headers do not obey all of these guidelines;
Or you can try using brace initialization:
Code:
std::vector<std::string> mylist {};
By the way, typedefs are not required when declaring/defining a struct in C++.
Code:
struct namefile
{
...
};
 
1 members found this post helpful.
Old 04-01-2018, 12:53 AM   #3
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
your sturct is classical C and has nothing todo with how C++ is supposed to do.
it just compiles because C++ compiles 99.9% of all C code

-Weffc++ is a useless warning, some thought it would be a nice idea to check Scott Meyers rules, back in 99, but ... todays C++ is different.
 
1 members found this post helpful.
Old 04-01-2018, 08:08 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,916

Original Poster
Rep: Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396
Quote:
Originally Posted by a4z View Post
your sturct is classical C and has nothing todo with how C++ is supposed to do.
it just compiles because C++ compiles 99.9% of all C code

-Weffc++ is a useless warning, some thought it would be a nice idea to check Scott Meyers rules, back in 99, but ... todays C++ is different.
thanks , time to update that sh8, and, yes it started out as a fully working C file. Then I decided to try my hand at C++. As much as it's convenience that C++ uses C it does hinder some as well. I suppose my code would be an example.

Last edited by BW-userx; 04-01-2018 at 08:17 AM.
 
Old 04-01-2018, 08:09 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,916

Original Poster
Rep: Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396Reputation: 1396
Quote:
Originally Posted by norobro View Post
I think that warning can be ignored. From here:https://gcc.gnu.org/onlinedocs/gcc-7...meric-literals
Or you can try using brace initialization:
Code:
std::vector<std::string> mylist {};
By the way, typedefs are not required when declaring/defining a struct in C++.
Code:
struct namefile
{
...
};
Thanks, putting that into practice. Time to hit Google a little harder as well to give me what I'm looking for.

Last edited by BW-userx; 04-01-2018 at 08:11 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration