I am trying to make a simple linked list. Everything was going fine, and then all of a sudden, a massacre of errors. I have no clue what I changed to break my code. This is my file that is getting some of the errors:
#pragma once
#include <string>
#include "Node.h"
class LinkedList
{
private:
Node *head;
public:
LinkedList();
~LinkedList();
void AddNode(int);
string GetList(); //missing ';' before identifier 'GetList'
bool Contains(int);
void Remove(int);
};
It claims that I am missing a semi-colon on the line above string GetList();, or so it looks...but obviously I am not. That exact error is:
Error 1 error C2146: syntax error : missing ';' before identifier 'GetList' c:\...\linkedlist.h 15 1 ProjectName
The other error on that line is:
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\...\linkedlist.h 15 1 ProjectName
But it is identified as a string return type.
In LinkedList.cpp, this is the GetList() method:
string LinkedList::GetList(){
string list;
Node *currentNode = head;
while (currentNode->next_node){
currentNode = currentNode->next_node;
list += currentNode->get_value() + " ";
}
return list;
}
It all appears good there, but in the method header, I am getting the following 2 errors:
Error 4 error C2556: 'std::string LinkedList::GetList(void)' : overloaded function differs only by return type from 'int LinkedList::GetList(void)' c:\...\linkedlist.cpp 28 1 ProjectName
Error 5 error C2371: 'LinkedList::GetList' : redefinition; different basic types c:...\linkedlist.cpp 28 1 ProjectName
I have gone so far as to create a new project and copy and paste all of my files back in, but that had no effect. I had successfully run GetList() in this program previously.
Does anyone know what in the world is going on here? My IDE is lying to me! (Visual Studio Community 2013 Update 4)
You have using namespace std; somewhere in LinkedList.cpp, but not in LinkedList.h. That's why in the class definition it doesn't know you're referring to std::string when you write string.
I'd recommend to stop using using namespace std; to avoid this kind of problems.
Use std::string, not just string.
Related
I am trying to create a inventory linked list where the user can add a product(id,info,price,count) and then to store the object in a linked list.
The issue I am having is with the Node class giving error "missing type specifier - int assumed. Note: C++ does not support default-int" and so on for each statement in the Node Class.
#ifndef INVENTORY_H
#define INVENTORY_H
#include<iostream>
#include <string>
#include <iomanip>
using namespace std;
class Node
{
public:
Node(){}
Node(const Inventory& theData, Node *theLink)
: data(theData), link(theLink){}
Node* getLink() const { return link; }
Inventory getData() const { return data; }
void setData(const Inventory& theData)
{
data = theData;
}
void setLink(Node *theLink) { link = theLink; }
private:
Inventory data;
Node *link; //pointer that points to next node
};
class Inventory
{
public:
Inventory();
void addPart(int, string, int, int);
void findPart(int);
void toBinary();
void quit();
private:
int id;
int price;
string partInfo;
int partCount;
Node* first;
Node* last;
int count;
};
#endif
And the errors are:
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2143: syntax error : missing ',' before '&'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C2146: syntax error : missing ';' before identifier 'getData'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): warning C4183: 'getData': missing return type; assumed to be a member function returning 'int'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(17): error C2143: syntax error : missing ',' before '&'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(23): error C2146: syntax error : missing ';' before identifier 'data'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2065: 'Thedata' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2065: 'theLink' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2614: 'Node' : illegal member initialization: 'data' is not a base or member
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C2065: 'data' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(19): error C2065: 'data' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(19): error C2065: 'theData' : undeclared identifier
You're just looking at a standard case of the compiler not knowing what your custom types are because of the order you have defined them. Even if you forward declare a class (put "class Inventory;" somewhere up above), you can't use it for certain things. Declaring a member variable that is not a pointer is one of them.
However, if you invert the definitions (Inventory before Node) and forward declare Node ("class Node;"), your code will compile because you only have Node* in your Inventory class.
Check this answer for more details: https://stackoverflow.com/a/553869/128581
As pointed out, this is not the best design if this code is not strictly for learning. STL containers cover most common data structures (doubly linked and singly linked lists are two of them). stl::list and stl::forward_list respectively.
The C++ compiler reads the source code from top to bottom. When it gets to line 13, it hasn’t heard of an Inventory type. It thinks Inventory must therefore be the parameter name, and gets really confused.
The solution to this is to switch the order of the Node and Inventory classes, and to pre-declare the Node class before the start of the Inventory class, by inserting the following line
class Node;
before the start of class Inventory.
Switching the order has Inventory defined before Node, which is important because the compiler needs to know everything about an Inventory to construct a Node, since Nodes contain Inventorys. The extra line tells the compiler that there is a Node class, but not anything about what it is; this way you can use pointers to Nodes (or anything else which doesn’t require knowing the layout of a Node), but can’t do anything else with them until they’re fully defined. Since Inventory only uses pointers, this shouldn’t be a problem.
However, as described I don’t see why Inventory needs the node pointers at all; it seems like your Inventory class is what you called a product in your English description, and a product shouldn’t need to know about the rest of the data. You also might want to just use std::forward_list instead of trying to implement your own linked list type.
I'm trying to separate my c++ code to one header and one cpp file, but the interpreter showing some errors.
Here is my code:
Password.h:
#ifndef PASSWORD_H
#define PASSWORD_H
class Password {
private:
string aliasName_;
int hashOfPassword_;
public:
void setAliasName(string aliasName);
void setHashOfPassword(int hashOfPassword);
string getAliasName() { return aliasName_; }
int getHashOfPassword() { return hashOfPassword_; }
};
#endif
Password.cpp:
#include <string>
#include "Password.h"
using std::string;
void Password::setAliasName(string aliasName) {
aliasName_ = aliasName;
}
void Password::setHashOfPassword(int hashOfPassword) {
hashOfPassword_ = hashOfPassword;
}
Errors:
Error C2065 'aliasName_': undeclared identifier X\password.cpp 7
Error C2511 'void Password::setAliasName(std::string)': overloaded member function not found in 'Password' X\password.cpp 6
Error C3646 'aliasName_': unknown override specifier X\password.h 6
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int X\password.h 6
Error C2061 syntax error: identifier 'string' X\password.h 9
Error C3646 'getAliasName': unknown override specifier X\password.h 11
Error C2059 syntax error: '(' X\password.h 11
Error C2334 unexpected token(s) preceding '{'; skipping apparent function body X\password.h 11
Anyone have any ideas ?
You need to move using std::string before the declaration of your class:
#ifndef PASSWORD_H
#define PASSWORD_H
#include <string>
using std::string;
class Password {
…
and remove it from your .cpp file.
Also, you might want to use #pragma once instead of the traditional #ifndef/#define/#endif and finally you might want to make your argument and methods const when need be.
You need to move
#include <string>
using std::string;
To inside Password.h, you can then remove these two lines from Password.cpp.
Just add std:: to every string in your header file and remember, you should never use using or using namespace there! You should also include all the headers to the file in which you use them.
So I've tried to figure out what exactly the professor was writing on the board and how it answers the lab assignment we are to do.
This is the lab assignment:
Create a Hash Table and Hash map that holds all of the WORDS in the (given below) Declaration of Independence.
Handle collisions using the chain method. (Note we will not be modifying this table nor doing deletions!)
Programmatically answer the following questions:
What is the size of your hash table?
What is the longest collision (ie. Chain)
What is the most frequently used word and how did you determine it?
Create a (second) Hash Table that holds all of the LETTERS in the Declaration of Independence.
What is the size of your hash table
What letter has the longest collision?
And this is the pseudo-code with some modifications that I did to fix some errors:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
class Translate
{
string word;
public:
int trans(string word);
w = word.charAT(0); //gives a letter
return #num;
};
class HashTable
{
int size();
int collision();
int length();
char fword();
public:
Translate t;
list<string> hashTable[29];
bool insert(string word)
{
hashTable[t.trans(word)].push_back(word);
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
HashTable h;
open file f("hash.txt");
//h.insert(word)
while (!f.eof())
{
h.insert(f.word());
}
cout << h.size;
cout << h.collision.length;
cout << h.fword;
return 0;
}
The errors that I have are:
Error 15 error C1903: unable to recover from previous error(s); stopping compilation
Error 5 error C2014: preprocessor command must start as first nonwhite space
Error 4 error C2059: syntax error : 'return'
Error 13 error C2065: 'f' : undeclared identifier
Error 10 error C2065: 'file' : undeclared identifier
Error 8 error C2065: 'open' : undeclared identifier
Error 6 error C2143: syntax error : missing ';' before '}'
Error 1 error C2143: syntax error : missing ';' before '='
Error 11 error C2146: syntax error : missing ';' before identifier 'f'
Error 9 error C2146: syntax error : missing ';' before identifier 'file'
Error 14 error C2228: left of '.eof' must have class/struct/union
Error 3 error C2238: unexpected token(s) preceding ';'
Error 7 error C2238: unexpected token(s) preceding ';'
Error 12 error C3861: 'f': identifier not found
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 19 IntelliSense: '#' not expected here
Error 17 IntelliSense: class "std::basic_string, std::allocator>" has no member "charAT"
Error 21 IntelliSense: expected a ';'
Error 18 IntelliSense: expected a declaration
Error 22 IntelliSense: identifier "f" is undefined
Error 20 IntelliSense: identifier "open" is undefined
Error 16 IntelliSense: this declaration has no storage class or type specifier
I've never used .c_str and I'm still pretty new to C++ so my knowledge is limited. I can tell that there are places that need an identifier but I think there is a better way to create a "open file". My previous knowledge is C#, HTML, and some Python in which C++ is giving me some difficulty in learning and understanding. Any help and/or insight would be greatly appreciated!
Code is too mangled to understand. However, I'm trying my best to help with the little knowledge of mine on C++ and hash.
Proposed Code Modification
Program entry point : instead of int _tmain(int, _TCHAR*), use int main().This should guarantee you the ability to test things out should you migrate to non-windows compiler.
Source : Unicode _tmain vs main
I would like to help with the remainder, however, the code posted is way too unintelligible. Would be kind if the algorithm is posted for reference.
There are a few things you should change:
Assuming trans() is supposed to be a function definition, not a declaration, and the lines following it are supposed to be the body:
Unless you specifically want to copy the passed string, you should use const string& instead of string.
It should have braces.
w is a char.
std::string defines operator[], so it can be indexed like an array.
I'm not sure what #num is (I assume it's from Python, but I'm not familiar with that), so I'm not sure how you intend to calculate the return value.
[I will thus assume that you want to return w, but as an int instead of a char. If this is the case, it would be simpler to just return word[0];.]
There are a few issues with HashTable's members.
Member functions size(), collision(), length(), and fword() are private. This doesn't appear to be intentional.
Member variables t and hashTable are public, when you likely wanted them to be private. Again, this doesn't appear to be intentional.
The functions aren't actually defined anywhere, unless you didn't show their definitions. This will cause a linking error when you call them.
While this doesn't need to be changed, there's no reason for HashTable::insert() to actually return a value, if it's hard-coded to always return true. Also, as mentioned in 1.1 above, the parameter should probably be const string&.
_tmain() and _TCHAR are a Microsoft extensions, which is available on Visual Studio and some (but not all) compilers aiming for compatibility with it (such as C++Builder). If you want your code to be platform-independent, you likely want main(). [Note that this doesn't need to be changed. If you're only compiling with Visual Studio, you can leave it as is. If you want platform independence, you can easily define _tmain and _TCHAR yourself.]
Opening a file:
Neither open nor file are keywords in C++, nor are they types (although FILE is a C type, it doesn't appear to be what you want). You appear to want std::ifstream.
You shouldn't use !f.eof() as a condition in a while loop, because eofbit won't be set until after reading fails.
fstream has no member function word(). However, the extraction operator, operator>>() will read a single word at a time, if given a parameter that can accept one.
HashTable::size(), HashTable::collision(), HashTable::length(), and HashTable::fword() are functions. To call them, you use operator(). If you just use a function's name directly, you don't call it, but instead refer to it (this can be used to create a function pointer or function reference).
int has no member function length(). Therefore, you cannot call h.collision().length(). In C++, if you chain function calls like that, each function in the chain is treated as if it were a member function of the directly preceding type, not the leftmost type; this means that for every function after the first, the return type of the preceding function is used. (In this case, h.collision() returns an int, so .length() attempts to call member function int::length(). int isn't a class type, and thus doesn't have any member functions.)
So, considering these, your code can be modified as follows:
// Assuming your stdafx.h contains "#include <string>" and "#include <tchar.h>".
// If it doesn't, either put them there, or #include them here.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>
// #4: Defining _tmain and _TCHAR
#ifndef _tmain
#define _tmain main
typedef char _TCHAR;
#endif
using namespace std;
class Translate
{
string word;
public:
// #1: Fixing trans().
int trans(const string& word)
{
char w = word[0]; // First letter of word.
return w; // Will be promoted to int.
}
};
class HashTable
{
// #2: Making member functions public, and member variables private.
Translate t;
list<string> hashTable[29];
public:
int size();
int collision();
int length();
char fword();
// #3: Making word a const reference. Changing return type to void.
void insert(const string& word)
{
hashTable[t.trans(word)].push_back(word);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
HashTable h;
// #5.1: Opening the file.
ifstream f("hash.txt");
//h.insert(word)
// #5.2 & 5.3: Reading a word.
std::string word;
while (f >> word)
{
h.insert(word);
}
// #6: Calling functions.
cout << h.size();
cout << h.collision(); // #7: Assuming you wanted to output both h.collision() and
cout << h.length(); // h.length(), I put them on separate lines.
// If you actually DID want h.collision().length(), then
// h.collision() should return a type (or reference to a type)
// with member function length(), or be an instance
// (or reference to an instance) of a class with member function
// length() (instead of being a function).
cout << h.fword();
return 0;
}
You still need to provide bodies for HashTable's member functions, apart from insert(), as well as make any other modifications you desire. You might also want to remove member word from Translate, if it doesn't actually need to store a string.
When I go to build my C++ project, I get 53 errors. However, it's the same list of errors 4 times in a row from one of the 5 header files I have in my project. I checked the output and found that it attempted to compile that one header file 5 times. It appears that the first time was successful. The other 4 times got errors, but they were the same errors over and over again. I followed where the includes of lead to. Based on all of the places that I include that header file, it makes sense that it would try to compile it for every time that it's included.
This is the header file that's getting compiled multiple times. The first successful compile makes sense, but I don't understand why it's getting a bunch of errors every other time it compiles while building the project:
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <string>
#include "Account.h"
#include "BSTree.h"
using namespace std;
class Transaction
{
public:
Transaction();
Transaction(char type, string firstName, string lastName, int ID, Account* account1, int fund1, Account* account2, int fund2, int amount);
~Transaction();
void setPtrAccounts(BSTree* ptrAccounts);
bool Transact();
private:
static BSTree* ptrAccounts;
char type;
string firstName;
string lastName;
int ID;
Account* account1;
int fund1;
Account* account2;
int fund2;
int amount;
void Deposit();
void History();
void Open();
bool Transfer();
bool Withdraw();
};
#endif
Here's the repeating list of errors. These errors are completely bogus. There's nothing wrong with the code in the above header file:
error C2061: syntax error : identifier 'Account' \thejollybanker\transaction.h 14 1
error C2061: syntax error : identifier 'BSTree' \thejollybanker\transaction.h 16 1
error C2143: syntax error : missing ';' before '*' \thejollybanker\transaction.h 19 1
error C4430: missing type specifier - int assumed. \thejollybanker\transaction.h 19 1
error C2143: syntax error : missing ';' before '*' \thejollybanker\transaction.h 24 1
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int \thejollybanker\transaction.h 24 1
error C2143: syntax error : missing ';' before '*' \thejollybanker\transaction.h 26 1
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int \thejollybanker\transaction.h 26 1
Here's a summary of the output window:
Transaction.cpp
TheJollyBanker.cpp
Transaction.h errors
Fund.cpp
BSTree.cpp
Transaction.h errors
Bank.h
Transaction.h errors
Account.cpp
Transaction.h errors
Generating Code...
How do I get it to only compile it once so that it successfully compiles the first time?
string is part of namespace std. Replace string with std::string everywhere in that header, then it should work.
In my c++ program i'm trying to create a dll that houses the functionality of my a* algorithm.
I encounter a problem when trying to pass the map into it, I first tried to use a 2d array, but that limited my map sizes, so i'm now trying to use a vector in a vector and I keep hitting some odd snag.
In my dlls .h file:
namespace IInterface
{
class IInterface
{
public:
// Sets the map
static __declspec(dllexport) void setMap(int h, int w,vector<vector<byte>> &myarray);
private:
static vector<vector<byte>> mymap;
}
Finaly in the .cpp i have:
#include "IInterface.h"
#include <Windows.h>
#include <stdexcept>
#include <vector>
using namespace std;
namespace IInterface
{
void IInterface::setMap(int h, int w,vector<vector<byte>> &myarray)
{
mymap = myarray;
}
}
Im getting a few errors on compilation even tho the code looks fine to me.
error C2061: syntax error : identifier 'vector' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 7 1 DMAstarDLL
error C2143: syntax error : missing ';' before '<' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2238: unexpected token(s) preceding ';' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2511: 'void IInterface::IInterface::setMap(int,int,std::vector<_Ty> &)' : overloaded member function not found in 'IInterface::IInterface' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.cpp 13 1 DMAstarDLL
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
I looked at some samples, but there was really nothing that matched this scenario. I have a sneaking suspicion i'm forgetting something crucial, but I cant see it. Ideas on getting this to work?
your dlls.h does not include vector type - you should tell the compiler vector definition and include .
Tip: don't use using namespace std; in header file only in cpp. Instead of this use std::vector ...etc.
Secondly, be careful when your dll interface contains stl. This library differs as regards Release and Debug versions, so if you load Release dll in Debug program you could have problems.