Visual Studio and GNU compiler question -fpermissive

I dual boot between Linux Mint and Windows 7. I prefer to write code in Linux (Codeblocks for IDE), just because it's faster in general. But, I'm taking C++ courses, and when doing assignments, occasionally I'll be kind of forced to use Visual Studio just because I can get a lot of errors in certain cases, such as when using string class.

When I use string class in Linux, I get many errors and warnings with -fpermissive tags on them.

When the exact same code is run in Visual Studio, it returns zero errors and zero warnings.

I'm curious about who is right here, I'm going to assume GNU follows the standard a little better than Microsoft.

If I enable the -fpermissive in GNU, is it going to let me get away with writing bad code, or is there another way I can get both compilers to follow the same standards?

Here is a class I wrote that compiles just fine in VS but doesn't compile at all with GNU.

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
 
#include  <iostream>
#include  <fstream>
#include  <string>
#include  <cstring>
#include  <cstdlib>
#include  <iomanip>
#include  "dict.h"


using namespace std;

Dictionary::Dictionary(const string &filename)
{
ifstream  in(filename, ios::in);

  if  (in.fail())
  {
    cout  <<  "Error, file failed to open: "
          <<  filename
          <<  endl;
  }

in >> wordNum;
in.ignore();

get_memory();
load_array(in);
sort_array();

}

string  Dictionary::lookupDefinition( const string  &word )
{
	words_def  key;
    key.word  =  word;
	words_def *resultPtr;

	resultPtr  =  (words_def *) bsearch( &key, word_def_array, wordNum, sizeof(words_def), compare );

	if (resultPtr!=NULL){
    return (resultPtr->def);
	} else {
	return ("is not in this dictionary. Try a different word. ");
	}
}

int Dictionary::compare( const void  *vp1, const void  *vp2 )
{
	const words_def  *p1  = (const words_def *)  vp1;
	const words_def  *p2  = (const words_def *)  vp2;

	return (p1->word.compare(p2->word));
}

void Dictionary::sort_array()
{
	qsort( word_def_array , wordNum , sizeof(words_def) , compare);
}

void Dictionary::load_array(ifstream &in)
{
 for (int inc = 0 ; inc < wordNum ; ++ inc)
  {
     getline ( in, word_def_array[inc].word, ',');
     getline ( in, word_def_array[inc].def);
  }
in.close();
}

void Dictionary::get_memory()
{
word_def_array = new words_def[wordNum];

	if  (! word_def_array){
    cout  <<  "Failed to allocate memory."  <<  endl;
    exit( EXIT_FAILURE );
    }
}

void Dictionary::forEach( WordDefFunc  func, bool  forward )
{
	if (forward == true){
		heading();
        for ( int i=0; i<wordNum; ++i )
            func(word_def_array[i].word, word_def_array[i].def) ;
    } else {
		heading();
		for ( int i = wordNum -1 ; i >= 0 ; --i ) {
			func(word_def_array[i].word, word_def_array[i].def) ;
	    }
	}
}

void Dictionary::heading()
{
	cout << left << setw(10) << "**Word**" 
		 << setw(30) << "**Definition**"
		 << endl << endl;
}

Dictionary::~Dictionary()
{
  delete [] word_def_array;
}

Last edited on
Not being able to see the full definiton of Dictionary, and not knowing the types of the member variables it's hard to say for sure what is wrong with your code. You not posting the actual error messages also makes it kind of hard to know what they were.

Using void* in a compare function in C++? Definitely questionable. Using c-style casts to suppress compiler warnings? Definitely questionable. bsearch in c++? Questionable.


If I enable the -fpermissive in GNU, is it going to let me get away with writing bad code, or is there another way I can get both compilers to follow the same standards?

Is there some reason you can't read your compiler's documentation or google fpermissive?

http://stackoverflow.com/questions/8843818/what-does-the-fpermissive-flag-do
Last edited on
> If I enable the -fpermissive in GNU, is it going to let me get away with writing bad code

Yes.

Even if you do not specify -fpermissive, by default gcc lets you get away with incorrect code.

Specify both -std=c++11 and -pedantic-errors if you want gcc to reject incorrect code.
Use something like > g++ -std=c++11 -Wall -Wextra -pedantic-errors

And with the Microsoft offering, specify the /Za compiler option.
Cire, I know that using bsearch and void pointers aren't the best thing in c++, this is a chunk of an assignment in my c++ class where I have to use these things as it's specifically required to use in the assignment. I have it finished and it works, I was just trying to ask why Visual Studio allows me to get away with certain things while g++ does not (It was late when I wrote this question, I probably did a poor job stating my question). I understand what -fpermissive does to an extent, I was just wanting to know if using that would get me closer to Visual Studio standards, or just let me get away with bad code (or both). This question was more for the sake of differences between compilers.

JLBorges, thanks for that. I will try using those in both compilers. I want to make sure the code I'm writing is the best it can be.
Topic archived. No new replies allowed.