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.
Related
I have to calculate the longest prefix string in the program. I am using c++ for this and I don't actually know extensively about vector and its functions.
The code is:
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
class Solution {
public:
void longestCommonPrefix(vector<string>& strs)
{
string ans;
strs.size(); //no of rows
strs.push_back("flower");
strs.push_back("flower");
string one=strs[0];
string two=strs[1];
int oneL=one.length();
int twoL=two.length();
int min=oneL<twoL?oneL:twoL;
for(int i=0;i<min;i++)
{
char temp=one[i];
if(temp==two[i] )
ans=ans+temp;
}
}
};
This displays an error of winMain and I do understand it must be related to the main function but the problem is I cannot put nain() function here else it displays an error again. Thus, I cannot put a main function and I if I don't I face an error.
Help me out stackmates.
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.
I'm running into an irritating problem where my program keeps crashing if I try to reference a private variable that I have created in one of my classes. I can't figure out where I am going wrong. Here is the class that calls the class that crashes:
#include <stack>
#include <fstream>
#include <ostream>
#include <cstdlib>
#include <string>
#include <set>
#include "schemeList.cpp"
using namespace std;
class dataLog
{
public:
stack<string> commands;
set<string> domain;
processor tokens;
int nextToken;
schemeList * s;
dataLog(stack<string> s, ofstream * out, processor p, int location)
{
commands = s;
tokens = p;
nextToken = location;
commands.push("<Query List>");
commands.push(":");
commands.push("Queries");
commands.push("<Rule List>");
commands.push(":");
commands.push("Rules");
commands.push("<Fact List>");
commands.push(":");
commands.push("Facts");
commands.push("<Scheme List>");
commands.push(":");
commands.push("Schemes");
checkNext();
}
void checkNext()
{
for(int i = 0; i < tokens.tags.size(); i++)
{
if(commands.top().compare(tokens.tags[i].getName())!=0)
{
if(commands.top().find("<")==0)
{
if(commands.top().compare("<Scheme List>")==0)
{
int output = (*s).process(i, tokens, domain); string hi = (*s).toString();
}
}
}
commands.pop();
}
}
};
This class creates an object of my SchemeList class, which is written out as follows:
#include "schemes.cpp"
#include <cstdlib>
#include <string>
#include <set>
using namespace std;
class schemeList
{
private:
string success;
public:
int process(int number, processor p, set<string> domain)
{
success = "HELLO";
return 13;
}
string toString()
{
return success;
}
};
As soon as I get to line 15 success = "HELLO";, the program crashes with the message
Unhandled exception at 0x00E48B66 in lab2.exe: 0xC0000005: Access violation reading
location 0xCCCCCCE4.
I am using Microsoft Visual Studio Express 2012 for Windows Desktop.
First off, the variable schemeList * dataLog::s is never initialized, so accessing it is undefined behavior, which leads to the crash. Most likely calling process on a dangling pointer and attempting to write into some memory you don't own.
Second, don't #include "schemeList.cpp". You're not supposed to include cpp files. Rather, separate declarations & implementations and include a header.
You have not initialized dataLog::s. When you call (*s).process(i, tokens, domain), you get undefined behavior.
Firstly, you're apparently including source code files in headers. This will likely break the one definition rule and go horribly wrong.
Secondly, 's' is not a very good name for a class member. It makes it almost impossible to find uses of it.
Thirdly, I can see nowhere in your code that initialises s. I can see where it gets referenced OK, but as it hasn't been initialised, the effect of dereferencing is undefined, and with luck will merely crash your program, which looks like what is happening.
Just recently began to get back to programing and started doing some exercises but i keep gettin an error that should be simple to solve but cant seem to solve it...
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
int numberInts = 3;
int strSize = 0;
char interator = 'o';
string source[3];
string strTest ("this is a test");
//source = (string*) malloc (3+1);
source[0] = "(a+(b*c))"; //abc*+
source[1] = "((a+b)*(z+x))";
source[2] = "((a+t)*((b+(a+c))^(c+d)))";
for(int i=0;i<numberInts;i++)
{
strSize = source[i].size();
for(int j = 0; j < strSize; j++)
{
iterator = strTest[0];
if(source[i][j] == '\(')
{
cout<<"\(";
}
}
cout << "\n";
}
return 0;
}
the line "iterator = strTest[0];" gives me a missing template argument error, and i cant really figure out why cant i assign to a char a position of a string that returns a char...
thanks
For one thing, you misspelled iterator as interator when you declared it.
Spelling mistake, your char variable is called interator not iterator.
Switch to Clang. It's error messages are much more specific. It actually catches most spelling mistakes and offers suggestions to what it thinks you might have meant. However it probably wouldn't have caught this as a spelling error because of the iterator template.
You would have seen the following as an error:
testclang.cpp:8:5: error: cannot refer to class template 'iterator'
without a template argument list
iterator = 5;
^
In file included from testclang.cpp:1:
In file included from /usr/include/c++/4.4/iostream:39:
In file included from /usr/include/c++/4.4/ostream:39:
In file included from /usr/include/c++/4.4/ios:40:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:67:
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:103:12: note:
template is declared here
struct iterator
However without the `using namespace std', this (testclang.cpp):
int main()
{
int interator = 3;
iterator = 5;
}
When compiled with clang:
clang testclang.cpp
produces:
testclang.cpp:4:5: error: use of undeclared identifier 'iterator'; did
you mean 'interator'?
iterator = 5;
^~~~~~~~
interator
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.