Error: variable or field 'functionName' declared void - c++

I have the following in a C++ header file, as shown below.
cmdtree.h:
#ifndef CMDTREE_H
#define CMDTREE_H
#include <iostream>
#include <fstream>
#include <string>
#include "functions.h" // different header where some functions are
#include "classes.h" // different header where classes are
using namespace std;
void printLine(string filename, int line);
void cmd_ask_name();
void cmd_look_suspect();
#endif /* CMDTREE_H */
Then, in a .cpp source file, I have the following typed exactly as below (with some filler from other extraneous functions cut out for brevity). There are no includes, nothing else but what's shown here. I'm wondering, also, if that was the right decision, but I've tried it several different ways so I just thought I'd give you the cleanest version of the error:
cmdtree.cpp:
void printLine(string filename, int line)
{
char descrip[11][500] = {0};
char *ch_arr = descrip[0];
fstream text;
int y = 0;
cout << "\nfile: " << filename << " line: " << line << endl;
text.open(filename,ios::in);
if (!text.is_open()) {
cout << "\nCould not open " << filename << "." << endl;
} else {
while (!text.eof())
{
ch_arr = descrip[y];
text.getline(ch_arr,500);
y++;
}
}
text.close();
ch_arr = descrip[line];
cout << " " << ch_arr << endl;
}
void cmd_ask_name()
{
}
void cmd_look_suspect() // look suspect
{
}
Upon compilation, I receive the following errors. The cause isn't obvious to me -- I've included in the header file. I even get the error when I include string at the top of the cpp file itself.
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f "build/Debug/Cygwin_4.x-Windows/cmdtree.o.d"
g++ -c -g -std=c++11 -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/cmdtree.o.d" -o build/Debug/Cygwin_4.x-Windows/cmdtree.o cmdtree.cpp
cmdtree.cpp:3:16: error: variable or field 'printLine' declared void
void printLine(string filename, int line)
^
cmdtree.cpp:3:16: error: 'string' was not declared in this scope
cmdtree.cpp:3:33: error: expected primary-expression before 'int'
void printLine(string filename, int line)
^
nbproject/Makefile-Debug.mk:70: recipe for target 'build/Debug/Cygwin_4.x-Windows/cmdtree.o' failed
make[2]: *** [build/Debug/Cygwin_4.x-Windows/cmdtree.o] Error 1

It appears that you're missing the following from your cmdtree.cpp:
#include "cmdtree.h"
It doesn't get included automatically just because it has the same root file name as the .cpp file.
Also, it's not a good idea to put "using namespace std;" in a header file. Rather explicitly use std::string in the header and then you can put "using namespace std;" in the .cpp file.

Related

The #endif for this directive is missing

Currently creating a header file, and using the ifndef, define, endif, etc to create it.
However, every time I create it, Visual Studio Code is throwing errors.
#ifndef _roundemup_h_
#define _roundemup_h_
#include <iostream>
#include <fstream>
#include <string>
void file_name(std::ifstream &input)
{
std::string filename;
while (true)
{
std::cout << "Please enter a file name: " << std::endl;
getline(std::cin, filename);
input.open(filename);
if (input.is_open())
{
break;
}
std::cerr << "Unable to Process File." << std::endl;
}
}
#endif
Current errors being show:
the #endif for this directive is missing[1,2]
unrecognized preprocessing directive[24,2]
expected a declaration[25,1]
I'm copying this from a header I've made from another program but have changed the header name and ifndef, so you would think there would be no issue?
It seems clear that the compiler is first complaining that the endif is somehow deficient. First step should be a hex dump of the file to see if there are any weird characters in there, or a missing newline.
If in Linux, you can use something like:
od -xcb myfile.h

Error while building project in Eclipse

When I compile a C++ project in eclipse it shows me errors saying that all functions in IO.cpp are already defined.
This is my code:
File: IO.cpp
#include <string>
#include <iostream>
using namespace std;
void print(string line) {
cout << line;
}
void println(string line) {
cout << line << endl;
}
void printError(string message, string error, string file) {
cout << "An error occurred!" << endl;
cout << "Message: "+ message << endl;
cout << "Error: "+ error << endl;
if(file != "") {
cout << "File/Method: "+ file << endl;
}
}
File: main.cpp
#include <string>
#include <iostream>
#include "IO.cpp"
using namespace std;
int main()
{
println("Hello world!");
}
You should remove the line below from main.cpp
#include "IO.cpp"
And add following lines afer using namespace std
void print(string line);
void println(string line);
void printError(string message, string error, string file);
If you include a cpp file again which is also present in your project source list (files to be compiled), your program will get multiple definitions for the same functions which is not allowed in C++. On the other hand, the alternative suggested here consists of declarations of function, which is allowed to present several times, but must present atleast once before first use.
Standard practice is to move the declarations in a header file (for ex: IO.h) and include this header file in both IO.cpp and main.cpp
Further readings:
Difference between declarations and definitions
You included module IO.cpp in module main.cpp
#include "IO.cpp"
So you have gotten the function definitions in two modules: IO.cpp and in main cpp
You should create a header file for example IO.h and place there all function declarations. Then you have to include this header file in IO.cpp and main.cpp
For example
IO.h
#include <string>
void print( std::string line);
void println( std::string line);
void printError( std::string message, std::string error, std::string file);
IO.cpp
#include <string>
#include <iostream>
#include <IO.h>
using namespace std;
void print(string line) {
cout << line;
}
void println(string line) {
cout << line << endl;
}
void printError(string message, string error, string file) {
cout << "An error occurred!" << endl;
cout << "Message: "+ message << endl;
cout << "Error: "+ error << endl;
if(file != "") {
cout << "File/Method: "+ file << endl;
}
}
main.cpp
#include <IO.h>
//...

Using Multiple Files in C++ for functions and classes

I am trying to figure out how to make a program consisting of separate files. I read this post:
Function Implementation in Separate File
But have not succeeded. I have 3 files: main.cpp, func.cpp, time.h and when I compile, I get this error message:
duplicate symbol getOpen(std::basic_ofstream<char, std::char_traits<char> >&)in:
/var/folders/kp/57zkm0tn1q7b7w0cs7tlf98c0000gn/T//cczaW1Px.o
/var/folders/kp/57zkm0tn1q7b7w0cs7tlf98c0000gn/T//ccvOCRgc.o
ld: 1 duplicate symbol for architecture x86_64
collect2: ld returned 1 exit status
I have no idea what this means. I'm basically just trying to open a file, write to it, and close it. I also just created an object and tested it. I know the problem is from func.cpp because when I remove that it works. Can someone please advise? Thank you.
I type this to compile: g++ main.cpp func.cpp.
This is my code:
time.h
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
class Time
{
private:
int seconds;
int minutes;
int hours;
public:
Time(int=0, int=0, int=0);
Time(long);
void showTime();
};
Time::Time(int sec, int min, int hour)
{
seconds = sec;
minutes = min;
hours = hour;
}
Time::Time(long sec)
{
hours = int(sec / 3600);
minutes = int((sec % 3600)/60);
seconds = int( (sec%60) );
}
void Time::showTime()
{
cout << setfill('0')
<< setw(2) << hours << ':'
<< setw(2) << minutes << ':'
<< setw(2) << seconds << endl;
}
func.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int getOpen(ofstream& fileOut)
{
string filename = "outfile.txt";
fileOut.open(filename.c_str());
if( fileOut.fail())
{
cout << "\nFailed to open file.\n";
exit(1);
}
else
return 0;
}
main.cpp
#include <iostream>
#include "time.h"
#include "func.cpp"
int main()
{
ofstream outFile;
Time t1;
t1.showTime();
getOpen(outFile);
outFile << "This is a test" << endl;
outFile.close();
return 0;
}
You included "func.cpp" into main.cpp, so you have a double declaration of getOpen.
You shouldn't #include "func.cpp" into main.cpp.
Well, it depends on how you compile your program. If you compile only main.cpp, then you should include func.cpp. But that's not a nice way to write a program, and I hope you are not going to do that.
What you might want to do is to compile main.cpp and func.cpp separately (using gcc -c) and then link the .o files. That would be perfectly ok if you don't include one .cpp file into another. But when you include func.cpp into main.cpp, both of the .o files define getOpen function. That causes the error.
So: just remove the #include in main.

C++ Error C2653 'Class' is not a class or namespace name

Good afternoon. I've started learning c++ and I am having and issue compiling my project.
If you find some faulty code I would be glad if you tell me.
I have the following definitions:
Utils.h
#include "stdafx.h"
#include <string>
using namespace std;
class Utils
{
public:
static string GetStringFromInt (int number);
};
Utils.cpp
#include "Utils.h"
#include <sstream>
#include <string>
using namespace std;
string Utils::GetStringFromInt (int number)
{
stringstream ss;
ss << number;
return ss.str();
}
and
Ping.h
#include "stdafx.h"
#include <string>
using namespace std;
class Ping
{
public:
static int PingIt(int argc, char* argv[],string &mstime,string &ttl);
};
Ping.cpp
#include "Ping.h"
#include <string>
#include "icmpdefs.h"
#include <string>
#include <iostream>
#include <sstream>
#include <WinSock.h>
#include <Windows.h>
#include "Utils.h"
#pragma comment(lib,"wsock32.lib")
using namespace std;
int Ping::PingIt(int argc, char* argv[],string &mstime,string &ttl)
{
// Check for correct command-line args
if (argc < 2) {
cerr << "usage: ping <host>" << endl;
return 1;
}
// Load the ICMP.DLL
HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
if (hIcmp == 0) {
cerr << "Unable to locate ICMP.DLL!" << endl;
return 2;
}
// Look up an IP address for the given host name
struct hostent* phe;
if ((phe = gethostbyname(argv[1])) == 0) {
cerr << "Could not find IP address for " << argv[1] << endl;
return 3;
}
// Get handles to the functions inside ICMP.DLL that we'll need
typedef HANDLE (WINAPI* pfnHV)(VOID);
typedef BOOL (WINAPI* pfnBH)(HANDLE);
typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD,
PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no?
pfnHV pIcmpCreateFile;
pfnBH pIcmpCloseHandle;
pfnDHDPWPipPDD pIcmpSendEcho;
pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp,
"IcmpCreateFile");
pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp,
"IcmpCloseHandle");
pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp,
"IcmpSendEcho");
if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) ||
(pIcmpSendEcho == 0)) {
cerr << "Failed to get proc addr for function." << endl;
return 4;
}
// Open the ping service
HANDLE hIP = pIcmpCreateFile();
if (hIP == INVALID_HANDLE_VALUE) {
cerr << "Unable to open ping service." << endl;
return 5;
}
// Build ping packet
char acPingBuffer[64];
memset(acPingBuffer, '\xAA', sizeof(acPingBuffer));
PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAlloc(
GMEM_FIXED | GMEM_ZEROINIT,
sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer));
if (pIpe == 0) {
cerr << "Failed to allocate global ping packet buffer." << endl;
return 6;
}
pIpe->Data = acPingBuffer;
pIpe->DataSize = sizeof(acPingBuffer);
// Send the ping packet
DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]),
acPingBuffer, sizeof(acPingBuffer), NULL, pIpe,
sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 5000);
if (dwStatus != 0) {
cout << "Addr: " <<
int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
int(HIBYTE(HIWORD(pIpe->Address))) << ", " <<
"RTT: " << int(pIpe->RoundTripTime) << "ms, " <<
"TTL: " << int(pIpe->Options.Ttl) << endl;
mstime = Utils::GetStringFromInt((pIpe->RoundTripTime));
ttl = Utils::GetStringFromInt(int(pIpe->Options.Ttl));
}
else {
cerr << "Error obtaining info from ping packet." << endl;
}
// Shut down...
GlobalFree(pIpe);
FreeLibrary(hIcmp);
return dwStatus;
}
When I Compile the project I get:
Error 1 error C2653: 'Ping' : is not a class or namespace name c:\users\clanderasm\documents\visual studio 2010\projects\landetestconsole\landecplusconsole\ping.cpp 14 1 LandeCplusConsole
I've read sometimes this error is thrown because you dont include "stdafx.h"
on the first #include but I already changed it.
If you could tell me something more I would be glad
I couldn't reproduce your error with the code you gave, but I tried on VS2008 and some of the warning it raised make me think it could very much be due to your precompiled header not being included in sources. And there is a couple of other problems I can see that will surely cause you problems later:
Don't include precompiled header in .h. (Well you should even avoid including anything in your .h unless absolutely necessary). The precompiled header (at least in visual way of doing things) is meant to be included first in each cpp files (not .h). If you don't do so it will raise warnings for each includes you have like:
warning C4627: '#include "Ping.h"': skipped when looking for precompiled header use <- here you go your Point class is no longer defined!
and finally an error fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?.
It's actually messages I get when compiling your code on VS2008, maybe on VS2010 you have your error about Point not being defined in addition to those. When sorting out precompiled header problems it compiles fine (see next point)
Using a precompiled header does not mean the .h used to build it will be automatically included in all your sources. To do so, you have to change some project setting: right click on your project -> Properties, in the left panel, expand Configuration Properties -> C/C++ -> Advanced. Here on the list on the right you should see Force Includes. Type here stdafx.h, and voilà, you won't have to put it manually in each and every new .cpp you add to your project. Beware that you have to do that for all configuration (combo box on the top written "Configuration : Active(Debug)"
Apologies, still a VS2008 screen, hope it's the same on VS2010
Guard your headers. You should put gards on your headers to avoid multiple definitions of your classes when multiple include of the same .h happens (and it will).
You can do it 2 ways: the define method, and the pragma once method.
The pragma once is not standard but compiles faster on Visual, so you can eventually mix the 2 ways.
myheader.h using pragma once:
#pragma once
class MyClass
{
//<some definitions>
};
myheader.h using defines:
#ifndef __MYHEADER_H
#define __MYHEADER_H
class MyClass
{
//<some definitions>
};
#endif
myheader.h using both:
#pragma once
#ifndef __MYHEADER_H
#define __MYHEADER_H
class MyClass
{
//<some definitions>
};
#endif
It has already been said, but avoid the use of "using" in headers because it will spread. I myself avoid the use of "using" everywhere.

Returning a vector of strings

I'm a bit confused with all the namespaces for vector and how to properly return a vector of strings in my class. Here is the code:
main.cpp
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include "lab1.h"
using namespace std;
readwords wordsinfile;
words wordslist;
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) {
// Looks like we have no arguments and need do something about it
// Lets tell the user
cout << "Usage: " << argv[0] <<" <filename>\n";
exit(1);
} else {
// Yeah we have arguements so lets make sure the file exists and it is readable
ifstream ourfile(argv[1]);
if (!ourfile.is_open()) {
// Then we have a problem opening the file
// Lets tell the user and exit
cout << "Error: " << argv[0] << " could not open the file. Exiting\n";
exit (1);
}
// Do we have a ASCII file?
if (isasciifile(ourfile)) {
cout << "Error: " << argv[0] << " only can handle ASCII or non empty files. Exiting\n";
exit(1);
}
// Let ensure we are at the start of the file
ourfile.seekg (0, ios::beg);
// Now lets close it up
ourfile.close();
}
// Ok looks like we have past our tests
// Time to go to work on the file
ifstream ourfile2(argv[1]);
wordsinfile.getwords(ourfile2);
lab1.h
#ifndef LAB1_H
#define LAB1_H
bool isasciifile(std::istream& file);
class readwords {
public:
int countwords(std::istream& file);
std::vector<std::string> getwords(std::istream& file);
};
class words {
public:
void countall( void );
void print( void );
};
#endif
lab1.cpp
#include <fstream>
#include <iostream>
#include <map>
#include "lab1.h"
#include <vector>
using std::vector;
#include <string>
using namespace std;
vector<string> readwords::getwords(std::istream& file) {
char c;
string aword;
vector<string> sv;
int i = 0;
while(file.good()) {
c = file.get();
if (isalnum(c)) {
if(isupper(c)) {
c = (tolower(c));
}
if(isspace(c)) { continue; }
aword.insert(aword.end(),c);
} else {
if (aword != "") {sv.push_back(aword);}
aword = "";
i++;
continue;
}
}
return sv;
}
Here is the error from compiling.
g++ -g -o lab1 -Wall -pedantic main.cpp lab1.cpp
In file included from lab1.cpp:4:0:
lab1.h:9:4: error: ‘vector’ in namespace ‘std’ does not name a type
lab1.cpp:48:54: error: no ‘std::vector<std::basic_string<char> > readwords::getwords(std::istream&)’ member function declared in class ‘readwords’
make: *** [lab1] Error 1
Why do I get this error and how do I fix it. Thank you for any help you can provide.
Ryan
You have to #include <vector> in the header file as well. Actually, including it in the header is enough, as all files including that header will implicitly also include <vector>.
The thing is your include order is:
#include "lab1.h"
#include <vector>
and since you use std::vector in the header (before including it) you get the error. Reversing the include order would fix the compilation error, but doesn't solve the underlying error - that lab1 uses symbols that weren't defined yet. The proper fix is to include <vector>.
The compiler looks at code in the order it's written. That also applies to #include directives: the contents of the file are treated as if they had been written in the file that #include's them. As #LuchianGrigore has mentioned, the best solution is to add
#include <vector>
to "lab1.h". But you could hide the problem by moving the #include <vector> in "lab1.cpp" so that it comes before the #include "lab1.h". That would make the error go away, because the compiler would have already read` before it started to read "lab1.h". That's not what you should do, but it's the kind of thing that can happen accidentally and hide the actual problem.