G
Gary Wessle
Hi
I am getting a warning when I run this program, I am interested to
know why the compiler is warning.
thank you
**************** warning ****************
$ make clean; make
rm -rf *.o proj
g++ -c -o useful.o useful.cpp
useful.cpp: In member function 'int* count_rows_cols::get_counts()':
useful.cpp:31: warning: address of local variable 'x' returned
g++ -c -o useful_test.o useful_test.cpp
g++ -Wall -o proj useful.o useful_test.o
**************** useful.h ****************
#ifndef USEFUL_H
#define USEFUL_H
#include <string>
class count_rows_cols
{
int nRows, nCol;
std::string file_name;
void set_No_of_Rows_Cols();
public:
count_rows_cols(std::string const& fileName);
~count_rows_cols();
int* get_counts();
};
#endif
**************** useful_h.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "useful.h"
using namespace std;
count_rows_cols::count_rows_cols( string const& fileName )
: file_name( fileName ){
nCol = 0;
nRows = 1;
set_No_of_Rows_Cols();
}
void count_rows_cols::set_No_of_Rows_Cols() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );
string word;
while(input >> word)
nCol++; // init'd by constructor
while (getline(in, line))
nRows++; // init'd by constructor
}
count_rows_cols::~count_rows_cols() {}
int* count_rows_cols::get_counts(){
int x[2];
x[0] = nRows;
x[1] = nCol;
return x;
}
/*
std::string command = "wc -l";
std::system( ( command + " " + file_name ).c_str() );
}
*/
**************** useful_test.cpp ****************
#include <fstream>
#include <iostream>
#include <string>
#include "useful.h"
using namespace std;
int main() {
string f = "address.txt";
count_rows_cols x( f ); // space delimited
int* vd = x.get_counts();
cout << vd[0] << "\t" << vd[1] <<"\n";
}
I am getting a warning when I run this program, I am interested to
know why the compiler is warning.
thank you
**************** warning ****************
$ make clean; make
rm -rf *.o proj
g++ -c -o useful.o useful.cpp
useful.cpp: In member function 'int* count_rows_cols::get_counts()':
useful.cpp:31: warning: address of local variable 'x' returned
g++ -c -o useful_test.o useful_test.cpp
g++ -Wall -o proj useful.o useful_test.o
**************** useful.h ****************
#ifndef USEFUL_H
#define USEFUL_H
#include <string>
class count_rows_cols
{
int nRows, nCol;
std::string file_name;
void set_No_of_Rows_Cols();
public:
count_rows_cols(std::string const& fileName);
~count_rows_cols();
int* get_counts();
};
#endif
**************** useful_h.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "useful.h"
using namespace std;
count_rows_cols::count_rows_cols( string const& fileName )
: file_name( fileName ){
nCol = 0;
nRows = 1;
set_No_of_Rows_Cols();
}
void count_rows_cols::set_No_of_Rows_Cols() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );
string word;
while(input >> word)
nCol++; // init'd by constructor
while (getline(in, line))
nRows++; // init'd by constructor
}
count_rows_cols::~count_rows_cols() {}
int* count_rows_cols::get_counts(){
int x[2];
x[0] = nRows;
x[1] = nCol;
return x;
}
/*
std::string command = "wc -l";
std::system( ( command + " " + file_name ).c_str() );
}
*/
**************** useful_test.cpp ****************
#include <fstream>
#include <iostream>
#include <string>
#include "useful.h"
using namespace std;
int main() {
string f = "address.txt";
count_rows_cols x( f ); // space delimited
int* vd = x.get_counts();
cout << vd[0] << "\t" << vd[1] <<"\n";
}