Reputation: 527
At university I have to pass the course "Introduction to computer science". In this course they hand you different exercises on various topics, that you have to solve using assembly, c, c++.
My problem is, that I'm working with different compilers (g++ and Visual Studio C++), but they interpret the code I give them differently or require different code, so I have to implement my programs at least twice!
I'm looking for information, how I can write code that will work on gcc and Visual Studio C++.
Some examples of what I had already to deal with:
Working on VSC++:
template<typename T, template<typename X, typename Y> class container, class Allocator>
int in(int p_number, va_list params){
std::ifstream &data = *(va_arg(params, std::ifstream*));
if(!data.good()){
return -1;
}
char delimiter = (char)va_arg(params, int), temp;
std::stringstream ss;
while((temp = data.get())!=delimiter){
if(data.eof()){
return 0;
}
ss.put(temp);
}
T value;
ss >> value;
if(data.eof()){
return 0;
}
typedef container<T, Allocator>* con_p;
container<T, Allocator> &con = *(va_arg(params, con_p));
con.push_back(value);
return 1;
}
template<typename T>
struct MAP{
template<typename itr>
static int map(itr begin, itr end, int (*fp)(int, va_list), int p_number, ...){
int p_number2 = p_number+1;
va_list list;
va_start(list, fp);
int r=0, temp;
for(; begin!=end; ++begin){
p_number = reinterpret_cast<int>(&(*begin));
temp = (*fp)(p_number2, list);
if(temp==-1){
return -1;
}
r += temp;
}
va_end(list);
return r;
}
};
template<typename T,
template<typename X, typename Y> class container,
class Allocator>
int write(const container<T, Allocator> &con, std::string filepath, char delimiter = '\n'){
std::ofstream data;
data.open(filepath, std::ios::out | std::ios::binary | std::ios::trunc);
int r = MAP<T>::map(con.begin(), con.end(), out<T>, 2, &data, delimiter);
data.close();
return r;
}
Working in g++:
template<typename T, class A, template<typename X, typename Y> class container>
int write(const container<T, A> &con, std::string filepath, char delimiter = '\n'){
long temp; int r = 0;
std::ofstream data;
data.open(filepath.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
for(typename container<T, A>::const_iterator i=con.begin(); i!=con.end(); ++i){
if(!data.good()){
data.close();
return -1;
}
temp = (long)data.tellp();
data << *i << delimiter;
r += ((long)data.tellp() - temp);
}
data.close();
return r;
}
The same functionality, but very different implementations, because the template definitions, that are accepted by VSC++, aren't accepted by g++.
P. S. Only in case, if my university is searching the internet to find out, if their students are copying their homework out of the internet. My name is Christian W. student at TU-Darmstadt.
Upvotes: 1
Views: 87
Reputation: 980
Have you checked compiler messages? The error messages are produced for a reason. For me gcc
compiles the first example once appropriate headers are included (<cstdio>
, <iostream>
etc.) and types corrected (e.g. data.open(filepath.c_str(), ...
instead of data.open(filepath, ...
etc.).
Upvotes: 1
Reputation: 3830
Whenever you encounter this situation, it means your code is probably wrong. Compilers should properly handle correct code. Therefore, always check to see if you are making non-portable assumptions, or even have bugs that a more forgiving compiler would accept.
Having said that, in general when you want to do compilation for different platforms, you use conditional compilation blocks; that is, you enclose your code in
#ifdef PLATFORM_MACRO
// conditional code here
#endif
Where PLATFORM_MACRO is a #define that is set for the platform in question. The code in that block will then only be seen by the desired platform (ie: where PLATFORM_MACRO is defined).
Upvotes: 1