visual c++ LNK1120 LNK2019 error - visual-c++

I'm a newbie, maybe it's a trivial question.
This is my code:
#include <iostream>
using namespace std;
istream& func(istream&);
int main()
{
func(cin);
system("pause");
return 0;
}
istream& fumc(istream& is) {
int num;
while (is >> num, !is.eof()) {
cout << num<<endl;
}
is.clear();
return is;
}
I got some errors LNK1120 and LNK2019.
Can someone help me?
Thank you.

Function declaration and definition has mismatch name
define it as
istream& func(istream& is)
it will work.

I think it is a linker error. Add any additional dependencies in Configuration Properties->Linker.
Hope it helps.

Related

C++ Program doesn't show any output?

I am trying now to execute a C++ program in eclipse, but it doesn't show anything, my code is:
#include<iostream.h>
using namespace std;
int main()
{
cout<<"i have stucked here, plz do some thing for Me !";
return 0;
}
I think I am not ok in setting variable path. It is
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\QuickTime\QTSystem\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Samsung\Samsung PC Studio 3\;C:\soft\cygwin\bin
while my bin path is
c:\cygwin\bin
Try this:
#include<iostream>
using namespace std;
int main()
{
cout<<"I am stuck here, please do some thing for Me !";
return 1;
}
remove ".h" from header iostream.h because now .h forms are deprecated from c++ library., this is working fine for me..
#include<iostream>
using namespace std;
int main()
{
cout<<"I am stuck here, please do some thing for Me !";
return 0;
}

Unresponsive program with displaying a pressed key and using 'q' to exit program

I am following a tutorial video on how to make a very simple game using C++. I am very early in the tutorial and I had no issues until now. Accourding to the video when I run the program it should display any key I press with "Here's what you pressed: (pressed key goes here)". Also, it should exit the program when I press the Q key. On the video it works fine, but sadly on my screen it is just a blank DOS prompt that does not respond to anything. Can anyone please look at what I got so far and see if there is a way to troubleshoot this issue. Again, I am new at this so any help would be greatly appreciated. Perhaps there is a header missing or something...
game.cpp
#include <iostream> //Include this and namespace in all files.
using namespace std;
#include "game.h"
#include <conio.h>
bool Game::run(void)
{
char key = ' ';
while (key != 'q')
{
while (!getInput(&key))
{
}
cout << "Here's what you pressed: " << key << endl;
}
cout << "End of the game" << endl;
return true;
}
bool Game::getInput(char *c)
{
if (kbhit())
{
*c = getch();
}
return false;
}
game.h
#ifndef GAME_H //Make sure this accompanies #endif.
#define GAME_H
class Game
{
public:
bool run (void);
protected:
bool getInput (char *c);
void timerUpdate (void);
};
#endif //Make sure this accompanies #ifndef.
main.cpp
#include "game.h"
int main ()
{
Game gameHeart;
gameHeart.run();
return 0;
//system("pause");
}
The Game.getInput always returns false, so Game.run would endlessly ask for a keyboard input. Here's the fix.
bool Game::getInput(char *c)
{
if (kbhit())
{
*c = getch();
return true;
}
return false;
}
I suspect that the kbhit method is returning false and you are never prompted for key input. You can easily test this by commenting out that line so that getch() is guaranteed to be called.

Question about calling method inside custom IO operator in C++?

I have the following code:
#include "iostream"
#include "conio.h"
using namespace std;
class Student {
private:
int no;
public:
Student(){}
int getNo() {
return this->no;
}
friend istream& operator>>(istream& is, Student& s);
friend ostream& operator<<(ostream& os, const Student& s);
};
ostream& operator<<(ostream& os, const Student& s){
os << s.getNo(); // Error here
return os;
}
int main()
{
Student st;
cin >> st;
cout << st;
getch();
return 0;
}
When compiling this code, the compiler produced the error message: "error C2662: 'Student::getNo' : cannot convert 'this' pointer from 'const Student' to 'Student &'"
But if I made the no variable public and change the error line like: os << s.no; then things worked perfectly.
I do not understand why this happened.
Can anyone give me an explanation, please?
Thanks.
Because s is const in that method, but Student::getNo() isn't a const method. It needs to be const.
This is done by changing your code as follows:
int getNo() const {
return this->no;
}
The const in this position means that this entire method does not change the contents of this when it is called.

Inheritance and Combination - Base class has a pointer to derived class

I'm wondering if I can have two classes look like these:
//file: Small.h
#pragma once
#include "Little.h"
class Small :
public Little
{
public:
Small(void){}
~Small(void){}
};
and
//file: Little.h
#pragma once
#include <iostream>
#include "Small.h"
using namespace std;
//class Small;
class Little
{
public:
Little(){ s = 0; }
void print(){ cout << "oops!" << endl; }
Small* s;
};
And now my problem: When I wanna create an object of type "Small" and call its "print()" function, VS-2010 says that "class 'Small' has no member named 'print()'." What's the solution?
The following should work, however, you should be constructing classes in this manner, the base class should really have a member of the type of a derived class.
However as the member is just a pointer, the code should still compile and work, however your asking for errors.
The problem with you code is that the class Small must be defined before you create the class Little and Little bfroe you create Small
You Should do it in one file as follows, as they are interdependent
//file: SmallLittle.h
#pragma once
#include <iostream>
using namespace std;
class Small;
class Little
{
public:
Little(){ s = 0; }
void print(){ cout << "oops!" << endl; }
Small* s;
};
class Small :
public Little
{
public:
Small(void){}
~Small(void){}
};
However since Small is a member type of Little, your probably better of creating a single class as follows, The only reason for using your code is so you can use Little code in which Small is not defined, or used. However in this case your better off using a void * type for the pointer.
as Follows
//file: Small.h
#pragma once
#include <iostream>
using namespace std;
class Small;
class Small
{
public:
Small(){ s = 0; }
void print(){ cout << "oops!" << endl; }
Small* s;
~Small(void){}
};
Also you I don't think you need to set s to 0, as this is NULL, which should be the value until you create a pointer using new of assign a pointer.

vc++ - static member is showing error

I am using vc++(2010). I am trying to create a class for server side socket. Here is the header file
#include<winsock.h>
#include<string>
#include<iostream>
using namespace std;
class AcceptSocket
{
// static SOCKET s;
protected:
SOCKET acceptSocket;
public:
AcceptSocket(){};
void setSocket(SOCKET socket);
static void EstablishConnection(int portNo,string&);
static void closeConnection();
static void StartAccepting();
virtual void threadDeal();
static DWORD WINAPI MyThreadFunction(LPVOID lpParam);
};
SOCKET AcceptSocket::s;
and the corresponding source file
#include<NetWorking.h>
#include<string>
void AcceptSocket::setSocket(SOCKET s)
{
acceptSocket=s;
}
void AcceptSocket::EstablishConnection(int portno,string &failure)
{
WSAData w;
int error = WSAStartup(0x0202,&w);
if(error)
failure=failure+"\nWSAStartupFailure";
if(w.wVersion != 0x0202)
{
WSACleanup();
failure=failure+"\nVersion is different";
}
SOCKADDR_IN addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(portno);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
AcceptSocket::s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(AcceptSocket::s == INVALID_SOCKET)
failure=failure+"\nsocket creating error";
if(bind(AcceptSocket::s,(LPSOCKADDR) &addr,sizeof(addr)) == SOCKET_ERROR)
failure=failure+"\nbinding error";
listen(AcceptSocket::s,SOMAXCONN);
}
void AcceptSocket::closeConnection()
{
if(AcceptSocket::s)
closesocket(AcceptSocket::s);
WSACleanup();
}
void AcceptSocket::StartAccepting()
{
sockaddr_in addrNew;
int size=sizeof(addrNew);
while(1)
{
SOCKET temp=accept(AcceptSocket::s,(sockaddr *)&addrNew,&size);
AcceptSocket * tempAcceptSocket=new AcceptSocket();
tempAcceptSocket->setSocket(temp);
DWORD threadId;
HANDLE thread=CreateThread(NULL,0,MyThreadFunction,(LPVOID)tempAcceptSocket,0,&threadId);
}
}
DWORD WINAPI AcceptSocket::MyThreadFunction(LPVOID lpParam)
{
AcceptSocket * acceptsocket=(AcceptSocket *) lpParam;
acceptsocket->threadDeal();
return 1;
}
void AcceptSocket::threadDeal()
{
"You didn't define threadDeal in the derived class";
}
Now the main.cpp is
#include<Networking.h>
int main()
{
}
When I am compiling the error I got is
Error 1 error LNK2005: "private: static unsigned int AcceptSocket::s" (?s#AcceptSocket##0IA) already defined in NetWorking.obj C:\Documents and Settings\prabhakaran\Desktop\check\check\main.obj check
Error 2 error LNK1169: one or more multiply defined symbols found C:\Documents and Settings\prabhakaran\Desktop\check\Debug\check.exe 1 1 check
What might cause this and how do I solve it?
Put this in your .cpp file instead of in your .h file:
SOCKET AcceptSocket::s;
It is being included in many .cpp files if you have it in your .h file. And hence when you link it doesn't know which one to use.
Maybe a #pragma once at the very beginning of your header file will solve the problem.
The error message tells you that the linker finds multiple definitions of your class, obviously because you are including the header more than once.
That is fine in general, but then you should always add some so called inclusion guards in your header file to prevent this error.
EDIT:
Just saw that Brian R. Bondys answer is the correct one.
I'd like to elaborate on what Frank said. It's a common assumption that include guards might solve these kind of errors. Since the explanation got a bit lengthy, I've made a blog-post about it to explain the details:
http://daniel-albuschat.blogspot.com/2010/08/what-include-guards-in-c-are-and-what.html
Hope this is useful.

Resources