I keep getting the error "this declaration has no storage class or type specifier." I've checked for semicolons and I've included the relevant libraries.
#include <iostream>
#include <fstream>
using namespace std;
const int n = 8;
double p[n] = {1.23, .97, .86, .77, .69, .65, .71, .50};
double pi[n] = {.74, .70, .66, .68, .65, .62, .60, .54};
double z[n] = {.25, .35, .45, .55, .65, .75, .85, .95};
ofstream myfile;
myfile.open("ppilow.txt"); //Error here
Try to replace myfile.open("ppilow.txt"); with following code:
int main(int argc, char *argv[])
{
myfile.open("ppilow.txt");
return 0;
}
Explanation: you can't put instructions outside body of functions.
But it is much better to avoid declaration of global variables, so I recommend you to move it into body too.
Related
I'm working on some code and I've come across a few errors to do with defining/declaring and expecting a type specifier where my class name is?
I was wondering if anyone can simply explain to me where I've gone wrong and how to resolve these issues?
I've commented out the errors on each lines, Boot and Shoe and Footwear are classes in a header file Footwear.h
#include <iostream>
#include <string>
#include <vector>
#include "typedefs.h"
#include "Footwear.h"
int main(int argc, const char * argv[])
{
vector<Footwear*> collection; // vector and Footwear are undefined
Footwear* f; //f undefined
f = new Boot("Timbaland",10); //expected a type specifier
collection.push_back(f); //collection undefined
f = new Shoe("Brogue",5); //expected a type specifier
collection.push_back(f);
for (i = 0; i < collection.size(); i++) //i undefined
collection[i]>toString(); //toString undefined
return 0;
}
Use: std::vector
That may clear all the warnings. You've included but you still need to prefix the std namespace.
In your for loop, you must declare for(int i = 0....) you may want to use unsigned int since collection.size() should never be < 0.
Is there a way I can avoid using the atof function? How can I convert a string to float?
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
double x = 0;
for(int i=0; i<argc; i++)
x += atof(argv[i]);
cout<<x<<endl;
return 0;
}
You can use stringstream.
#include <iostream>
#include <sstream>
using namespace std;
int main () {
int val;
stringstream ss (stringstream::in | stringstream::out);
ss << "120";
ss >> val;
return 0;
}
For converting a string to a floating-point number in C++, it's now (since the standardization of C++11) advisable to use one of std::stof, std::stod and std::strold (where these functions had been added to the C++ standard library):
std::string s = "120.0";
float f = std::stof(s);
double d = std::stod(s);
long double ld = std::stold(s);
The primary reason to prefer these functions over those in the C standard library is safety:
they don't operate on raw pointers but string objects (that also leads to a minor improvement in convenience and readability: one does not have to call c_str() over and over again when working with std::strings); and
they don't exhibit undefined behavior when the conversion is impossible (they predictably throw well-documented exceptions instead).
You can use boost::lexical_cast to convert between written and numeric types in a way that's very idiomatic for C++.
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
using namespace std;
int main(int argc, char* argv[])
{
double x = 0;
for(int i=1; i<argc; i++)
x += boost::lexical_cast<float>(argv[i]);
cout<<x<<endl;
return 0;
}
I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -
..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope
Below is my C++ example
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str = "1234567";
uint64_t hashing = strtoull(str, 0, 0);
cout << hashing << endl;
}
return 0;
}
Is there anything wrong I am doing?
Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.
Try this for C++03 strtoull usage instead:
#include <string>
#include <cstdlib>
int main()
{
std::string str = "1234";
// Using NULL for second parameter makes the call easier,
// but reduces your chances to recover from error. Check
// the docs for details.
unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}
Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):
#include <string>
int main()
{
std::string str = "1234";
// See comment above.
unsigned long long ul = std::stoull( str, nullptr, 0 );
}
Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)
the structure for strtoull is: strtoull(const char *, char * *, int)
You have given it a std::string as pointed out by #juanchopanza
This is the solution I came up with is
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char str[] = "1234567";
unsigned long long ul;
char* new_pos;
charDoublePointer = 0;
ul = strtoull(str, &new_pos, 0);
cout << ul << endl;
return 0;
}
The output I got was: 1234567
Straight from the eclipse console.
Also at the end of your program you have return 0 out of scope with an extra curly brace.
On my mainfile, I wish to pass my loaded struct array from first function to second function ,both functions are in the source file(in.cpp). I was not able to do so even if I defined struct on main file, because firstly array cannot be return only pass by reference; I tried pass by reference also but does not work
#include "internetUsage.h"
#include "abc.h"
#include<fstream>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
char filename[20];
ifstream infile;
cout<<"enter file name: \n";
cin.getline(filename,20);
infile.open(filename);
newRecord(infile,filename);
updateRecord(infile,filename);
return 0;
}
This is my source file (in.cpp); after I loaded my struct array in my first function, I could not figure out how to display on my second function(updateRecord())
#include "internetUsage.h"
#include "abc.h"
#include <fstream>
#include<iostream>
using namespace std;
void newRecord(ifstream &infile,char filename[])
{
Info customer[50];
for(int i; i<6; i++)
{
infile>>customer[i].num;
infile>>customer[i].name;
infile>>customer[i].name2;
infile>>customer[i].charge;
}
}
void updateRecord(ifstream &infile,char filename[])
{
for(int j; j<6; j++)
{
cout<<customer[j].num<<endl;
cout<<customer[j].name<<endl;
cout<<customer[j].name2<<endl;
cout<<customer[j].charge<<endl;
}
}
This is my header file for two functions(internetUsage.h)
#include <fstream>
#include<cstring>
#include<iostream>
using namespace std;
void newRecord(ifstream &infile, char filename[]);
void updateRecord(ifstream &infile,char filename[]);
This is my header file for struct(abc.h)
#include<iostream>
using namespace std;
struct Info
{
int num;
char name[200];
char name2[200];
double charge;
};
you should define it Info customer[50]; as global variable , not local variable
define Info customer[50]; in main(),
Change newRecord() interface to add two parameters: Info * customer, int sizeCustomer
Change newRecord() internal loop to control customer's overflow, something like "if(j >= sizeCustomer) break;"
Change newRecord() interface to return number of customers have been read
Change updateRecord() interface to add two parameters: const Info * customer, int actualCustomerSize
Change updateRecord() internal loop to run for actualCustomerSize only, for example by modifing for(;j!=actualCustomerSize;)
Make changes in main()
Enjoy
Declare that structure as global in the file where you are loading that structure and then use the extern keyword along with the structure name in all the files where you want to use that function.
DO check this link:
http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
Here is my implementation file:
using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
#include <stack> //line 5
#include "proj05.canvas.h"
//----------------Constructor----------------//
Canvas::Canvas() //line 10
{
Title = "";
Nrow = 0;
Ncol = 0;
image[][100]; // line 15
position.r = 0;
position.c = 0;
}
//-------------------Paint------------------// line 20
void Canvas::Paint(int R, int C, char Color)
{
cout << "Paint to be implemented" << endl;
}
The errors I'm getting are these:
proj05.canvas.cpp: In function 'std::istream& operator>>(std::istream&,
Canvas&)':
proj05.canvas.cpp:11: error: expected `;' before '{' token
proj05.canvas.cpp:22: error: a function-definition is not
allowed here before '{' token
proj05.canvas.cpp:24: error: expected `}' at end of input
proj05.canvas.cpp:24: error: expected `}' at end of input
These seem like simple syntax errors, but I am not sure what's wrong. Could someone decode these for me? I'd really appreciate it, thanks for your time!
EDIT
Here is the definition of Canvas in my .h file:
#ifndef CANVAS_H
#define CANVAS_H
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
class Canvas
{
public:
Canvas(); void Paint(int R, int C, char Color);
const int Nrow;
const int Ncol;
string Title;
int image[][100];
stack<int> path;
struct PixelCoordinates
{
unsigned int r;
unsigned int c;
} position;
};
#endif
"proj05.canvas.h" i bet the problem is there. may be no ; after class def
You must use initializer list to initialize const-members
Try this:
#include <iostream>
#include <iomanip>
#include <string>
#include <stack> //line 5
#include "proj05.canvas.h"
using namespace std;
//----------------Constructor----------------//
Canvas::Canvas():Nrow(),Ncol() // Initializer list
{
Title = "";
//initialize image[][] correctly, your way is syntactically incorrect
position.r = 0; //correction here
position.c = 0; // and here
}
//-------------------Paint------------------// line 20
void Canvas::Paint(int R, int C, char Color)
{
cout << "Paint to be implemented" << endl;
}
Few things:
1
PixelCoordinates.r = 0;
PixelCoordinates.c = 0;
should be:
position.r = 0;
position.c = 0;
2
image has already been declared. What is this:
image[][];
It sounds like you forgot to put a semicolon after your class definition. Look in "proj05.canvas.h". You should see something like:
class Canvas{
...
};
One thing that catches my eye as wrong/weird is image[][]. That does not really do anything. Also, I do not believe you can assign to constant member outside of a ctor list.
Finally, your assignment to PixelCoordinates is completely in error. You've created a local struct definition, but have not made a member that uses it, therefore you cannot assign anything at all to it - especially the struct's title. That would really confuse a compiler.
Yikes.
(Not an answer to your specific problem, but...)
You should also remove the
using std;
That has no business in a .h file.
I am going to guess the oddly formatted .h file may be a problem. It is legal for a filesystem of course, but it could be that. Also ensure you have the ending semicolon on the class.
You need to have both dimensions filled in for the array you have (probably a horrible design to use that int he class anyway...)
Whatever the reason for other errors is, the memeber definition int image[][100] is illegal. Non-static data members of the class cannot be declared with incomplete types. All dimensions of an array must be specified explicitly.