The following code causes a C1001 internal error in Visual Studio 2013 (v12.0.30501.00 Update 2) - should I expect it to work? (downloadable here)
I was expecting to be able to call the func function without a vals argument and have the default of {10.0} used.
Any help appreciated!
C.hpp:
#include <string>
#include <initializer_list>
#pragma once
class C {
public:
void func(std::string str, std::initializer_list<double> vals = { 10.0 });
};
C.cpp:
#include "stdafx.h"
#include "C.hpp"
#include <iostream>
using namespace std;
void C::func(std::string str, std::initializer_list<double> vals){
cout << "str is " << str << endl;
for (double v : vals){
cout << v << endl;
}
}
initializer_list_default_parameter.cpp:
#include "stdafx.h"
#include "C.hpp"
int _tmain(int argc, _TCHAR* argv[])
{
C inst;
inst.func("name"); // this line causes a C1001 error with MSVC 2013
//inst.func("name", { 4.3 }); this line compiles
return 0;
}
Yes, initializer_list parameters can have default values, but there's a bug in the MSVC 2013 x86 compiler meaning they're not supported (http://connect.microsoft.com/VisualStudio/Feedback/details/925540).
Related
I have received the following error for the following code on Visual Studio Code for Mac.
ArrayStudy.cpp:21:19: error: cannot deduce type of initializer list because std::initializer_list was not found; include <initializer_list>
for (auto x : {10,21,32,43,54,65})
I included #include <initializer_list> after noting it was absent, but continued to receive the error. I'm using Apple clang version 13.0.0 (clang-1300.0.29.30).
At this point I am stumped. Can someone help me get this code running with a short explanation of what I missed?
#include <iostream>
#include <string>
#include <fstream>
#include <initializer_list>
void print();
int main(){
print();
}
void print() {
int v[] = {0,1,2,3,4,5,6,7,8,9};
for (auto x : v)
cout << x << '\n';
for (auto x : {10,21,32,43,54,65})
cout << x << '\n';
}
i am using eclipse on mac to run c++ program. I am new to c++ and was trying to learn composition by using different classes individually.I am facing the issue in the following line of the code
Main.cpp
#include <iostream>
using namespace std;
#include "Birthday.h"
#include "People.h"
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
Birthday obj(25,3,1993);
obj.print();
People pp(5,obj);
pp.printinfo();
return 0;
}
Birthday.cpp
#include <iostream>
using namespace std;
#include "Birthday.h"
//#include "People.h"
Birthday::Birthday(int d,int m,int y){
// TODO Auto-generated constructor stub
date =d;
month=m;
year=y;
}
void Birthday::print()
{
cout <<date << month<<year<<endl;
}
People.h
#ifndef PEOPLE_H_
#define PEOPLE_H_
//using namespace std;
#include "Birthday.h"
class People {
public:
People(int x,Birthday bb);
void printinfo();
private:
int xx;
Birthday bo;
};
#endif /* PEOPLE_H_ */
People.cpp
#include "People.h"
#include <iostream>
using namespace std;
#include "Birthday.h"
#include<string>
People::People(int x,Birthday bb)
:xx(x),bo(bb)
{
// TODO Auto-generated constructor stub
}
void People::printinfo()
{
cout<< xx<<bo.print(); //I am getting error because of this line , as soon as i comment it program compiles fine.
}
I have tried to use string variable instead of xx variable but it was giving me some other error.So i tried to simplify and learn the concept of compostion before jumping into strings manipulation directly.
cout << xx << bo.print();
bo.print() - function and they not have return value (void)
Just write:
cout << xx;
bo.print();
I wrote a pretty simple code for learning the concept of constructors.
I made a Project file in Dev-C++ 5.11, which uses the compiler TDM_GCC 4.9.2 32-bit Release.
Following is the code containing the main function:
#include <iostream>
#include "Classy1.h"
using namespace std;
int main()
{
Classy1 ao(3,4);
ao.printy();
return 0;
}
Here is the header file containing the class Classy1:
#ifndef CLASSY1_H
#define CLASSY1_H
class Classy1
{
private:
int v1;
int v2;
public:
Classy1(int a,int b);
void printy();
};
#endif
And here is the cpp file containing the constructor and the printy function:
#include "Classy1.h"
#include <iostream>
using namespace std;
Classy1::Classy1(int a,int b)
: v1(a), v2(b)
{
}
Classy1::printy()
{
cout << v1 << " " << v2;
}
Now, the compiler is showing an error on compilation:
You forgot to add a return type for the implementation of printy(). It should be:
void Classy1::printy()
{
cout << v1 << " " << v2;
}
I am trying to learn C++, however, the parameter to a method I have in my own class is misbehaving. When it uses a dataType of 'int', it works fine with no errors, but when I attempt to change it to a 'string' dataType, the program crashes with this error.
Error 1 error C2061: syntax error : identifier 'string' in temp.h ln
8 col 1
The classes I am using are as follows:
WORKING CODE
TesterClass.cpp // Entry Point
#include "stdafx.h"
#include "Temp.h"
int _tmain(int argc, _TCHAR* argv[])
{
Temp tmp;
tmp.doSomething(7);
return 0;
}
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(int blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(int blah)
{
std::cout << blah;
}
BROKEN CODE
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(string blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(string blah)
{
std::cout << blah;
}
When I adjust the parameter 'blah' to be a string, in both the .h and .cpp file, the problem occurs.
I have looked around, but none of the answers seem to solve my problem. I would greatly love help on this an I am out of ideas. I have tried reinstalling C++, messing with:
using namepace std;
using std::string;
std::string instead of string
etc.
If you know how to solve my problem I would love to hear from you. I am more than happy to provide more information.
C++ performs single-pass compilation, so std::string needs to be declared before you use it at all - including in the header file.
// Temp.h
#pragma once
#include <string>
class Temp
{
public:
Temp();
void doSomething(std::string blah);
};
I would encourage you to be specific in your header files when specifying classes like this, because you might easily come across another library that defines it's own string and then you would run into naming conflicts. Save the using import statements for your cpp files.
πάντα ῥεῖ had the write answer, thankyou!
They said to use std::string when needed, and to also #include <string> in the header file.
I can use array pointers as iterators in for_each but when I try to do the same using distance, the Solaris 12.4 compiler complains about not finding a match.
Here is my code:
#include <iterator>
#include <algorithm>
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::hex;
struct Bob {
char q[16];
};
Bob stuff[16];
void DoBob(Bob& bob) {
cout << "BOB! " << hex << &bob << endl;
}
int main() {
// int ret = std::distance(&stuff[0], &stuff[3]);
// The line above causes the Solaris C++ compiler to complain:
//"main.cc", line 22: Error: Could not find a match for std::distance<_ForwardIterator, _Distance>(Bob*, Bob*) needed in main().
std::for_each(&stuff[0], &stuff[3], DoBob);
return 0;
}
I tend not to like to blame the compiler. Am I doing something wrong or is this a problem with the Solaris compiler?