g++ Error help

Well.. here goes. I originally wrote this using visual c++ but now the requirement has changed that it must run on a *nix system. Whenever i attempt to compile using g++ main.cpp (which is what the file is named) i recieve the following errors.

I have already corrected a few by moving some declarations outside of main(), but i am at a loss on how to deal with the others.... the errors sound greek to me. ANY help would be grand.

Errors:
1
2
3
4
5
6
7
8
9
10
11
main.cpp: In function ‘int main()’:
main.cpp:53: error: missing template arguments before ‘fin’
main.cpp:53: error: expected ‘;’ before ‘fin’
main.cpp:55: error: ‘fin’ was not declared in this scope
main.cpp:62: error: invalid use of qualified-name ‘std::pos’
main.cpp:63: error: ‘pos’ is not a member of ‘std’
main.cpp:66: error: ‘pos’ was not declared in this scope
main.cpp:81: error: ‘struct std::string’ has no member named ‘front’
main.cpp:92: error: expected initializer before ‘:’ token
main.cpp:97: error: expected ‘)’ before ‘;’ token
main.cpp:143: error: cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*----------------------------------------------------------------------------------------------*/
//Name: 
//Date:
//Email:
//Email:
//
//
//		
//		
//		
/*----------------------------------------------------------------------------------------------*/

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cctype>
#include<cstdlib>

int row;
int listCount = 0;
std::string line ;
std::string file_path;

const char nullchar = 0 ;
const std::string space( 1, ' ' ) ;
const std::string three_spaces = space + space + space ;

std::vector< std::vector<std::string> > tokens_in_all_lines ;

// An array to hold the non numeric strings from the file - arbitrary size
std::string listArray[256];


bool is_number(const std::string& s)
{
    std::string::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}

int main()
{
	std::cout << "Name: " << std::endl;
	std::cout << "Date: " << std::endl;
	std::cout << "Email: " << std::endl;
	std::cout << "Email: " << std::endl<< std::endl;
	std::cout << "Please enter the full path to the file to be decrypted."<< std::endl;
	std::cout << "Path = ";
	std::cin >> file_path;
	std::cout << std::endl<<std::endl<<std::endl;
    	std::basic_ifstream fin(file_path) ;

	while( std::getline( fin, line) ) // while not EOF
    {
        // split into tokens
        std::vector<std::string> tokens_in_this_line ;

        // check for three consecutive spaces in this line indicating a space string
		// stringstream does not pick up consecutive spaces
        auto pos = line.find(three_spaces) ;
        if( pos != std::string::npos ) // if 3 spaces found
        {
            // before pos of three consecutive spaces
            std::string part_one = line.substr( 0, pos ) ;
			// after pos of three consecutive spaces
            std::string part_two = line.substr( pos + three_spaces.size() ) ;
			// replace the middle space of the three consecutive spaces
            // replaced with a nullchar
			//concatenate back to form a line
            line = part_one + space + nullchar + space + part_two ;
        }

        // parse this line
        std::istringstream stm(line) ;
		std::string temp ;
        while( stm >> temp )
        {
            // if nullchar, replace it with a space
            if( temp.front() == nullchar ) tokens_in_this_line.push_back(space) ;
            else tokens_in_this_line.push_back(temp) ;
        }

        // add extracted tokens to tokens_in_all_lines
        tokens_in_all_lines.push_back(tokens_in_this_line) ;

	}//get next line

    // determine the max number of columns
    std::size_t max_width = 0 ;
    for( const auto& tokens : tokens_in_all_lines )
        if( max_width < tokens.size() ) max_width = tokens.size() ;

	row = tokens_in_all_lines.size();
	//Display Encrypted File
	std::cout << "---------------Begin Encrypted Message-------------"<<std::endl;
	for(int r=0;r<row;++r)
		{
			for(int i=0;i<max_width;++i)
			{
				tokens_in_all_lines[r].resize(max_width);
				std::cout << tokens_in_all_lines[r][i];
			}
				std::cout << std::endl;
		}
	std::cout << "---------------End Encrypted Message---------------"<<std::endl;
	std::cout << std::endl<< std::endl<< std::endl;

	// Begin to Decrypt Message one string/token at a time
	std::cout << "---------------Begin Decrypted Message-------------"<<std::endl;

	for(int r=0;r<row;++r)
		{
			for(int i=0;i<max_width;++i)
			{
				tokens_in_all_lines[r].resize(max_width);

				//Make sure vector isnt empty, if it is.. skip that iteration. Empty vector errors out if attempted to be accessed
				if(tokens_in_all_lines[r][i]!="")
				{
					//Check If value is not a number
					if(!is_number(tokens_in_all_lines[r][i]))
					{
						if(listCount==0)
						{
							listArray[listCount]=tokens_in_all_lines[r][i];
						}
						else
						{
							for(int l=listCount+1;l>0;--l)
							{
								listArray[l]=listArray[l-1];
							}
							listArray[0]= tokens_in_all_lines[r][i];
						}
						listCount+=1;
						std::cout << listArray[0];
					}
					//If it was not a number then it must be a number
					else
					{
						int value = atoi(tokens_in_all_lines[r][i]);
						std::cout << listArray[value];
						std::string swap = listArray[value];
						for(int n=value;n>0;--n)
						{
							listArray[n]=listArray[n-1];
						}
						listArray[0]=swap;
					}
				}
			}
				std::cout << std::endl;
		}
	std::cout << "---------------End Decrypted Message---------------"<<std::endl;
	//std::cout << std::endl<< std::endl<< std::endl<< std::endl;

}
closed account (o3hC5Di1)
Hi there,

- Line 53: std::basic_ifstream fin(file_path) ; Why don't you use std::ifstream fin(file_path); ?

- Line 62: auto pos = line.find(three_spaces) ; You may want to try to declare pos as: size_t pos = line.find(three_spaces);

- Line 81: temp.front() front() is a c++11 function, which may not be supported by your compiler. Try: temp[0]

- Line 92: for( const auto& tokens : tokens_in_all_lines ) range based for-loops are also a c++11 feature, which may or may not be supported by your compiler. You can try a for loop using iterators.

- Line 143: tokens_in_all_lines is a std::vector<std::string>, atoi() expects a character array (c-string) as an argument. You can try atoi(tokens_in_all_lines[r][i].c_str());

Hope that helps.

All the best,
NwN
Fix lines 53 and 143. And then compile with

g++ -std=c++11 -O2 -Wall -Wextra -pedantic-errors main.cpp
Last edited on
will try both of the above thanks guys... i can tell i will have to update my g++
closed account (o3hC5Di1)
Ah yes, i confused myself by your statement of using visual c++. Apologies, like JLBorges said, using the -std=c++11 (or -std=c++0x) flag with g++ allows you to use c++11 features.

All the best,
NwN
OMG... update from 4.3.x on backtrack is proving to be a beast in itself!!!

geesh
Topic archived. No new replies allowed.